[PC-BSD Commits] r8198 - pcbsd/current/src-qt4/libpcbsd

svn at pcbsd.org svn at pcbsd.org
Sat Dec 11 11:50:07 PST 2010


Author: kris
Date: 2010-12-11 11:50:06 -0800 (Sat, 11 Dec 2010)
New Revision: 8198

Modified:
   pcbsd/current/src-qt4/libpcbsd/utils.cpp
Log:

Improve the Bytes to Human Readable function, now it works properly in more
cases :)



Modified: pcbsd/current/src-qt4/libpcbsd/utils.cpp
===================================================================
--- pcbsd/current/src-qt4/libpcbsd/utils.cpp	2010-12-11 10:01:43 UTC (rev 8197)
+++ pcbsd/current/src-qt4/libpcbsd/utils.cpp	2010-12-11 19:50:06 UTC (rev 8198)
@@ -57,30 +57,19 @@
 
 QString Utils::bytesToHumanReadable(long long bytes)
 {
-   QString suffix[] = {"B", "KB", "MB", "GB", "TB"};
-   long long remainder = 0;
-   int i = 0;
-   while ( bytes > 1024 && (i < 4) )
-   {
-     remainder = bytes % 1024;
-     bytes = bytes / 1024;
-     i++;
-   }
+   float num = bytes;
+   QStringList list;
+   list << "KB" << "MB" << "GB" << "TB";
 
-   QString pointval;
-   if ( i == 3 ) {
-     pointval = QString::number(remainder);
-     pointval.truncate(1);
-     pointval = "." + pointval;
-   } 
-   if ( i == 4 ) {
-     pointval = QString::number(remainder);
-     pointval.truncate(2);
-     pointval = "." + pointval;
-   } 
+   QStringListIterator i(list);
+   QString unit("bytes");
 
-   QString result = QString::number(bytes) + pointval;
-   return result += suffix[i];
+   while(num >= 1024.0 && i.hasNext())
+   {
+     unit = i.next();
+     num /= 1024.0;
+   }
+   return QString().setNum(num,'f',2)+" "+unit;
 }
 
 



More information about the Commits mailing list