Statistics::getUnitFactor and Statistics::getUnitString were not
consistent and difficult to understand. The readout displayed
incorrect values like 0.9kBps while the actual rate was 0.9MBps.
The current function will switch to next-higher rate value when rate
exceeds 512.0 units/s.
---
src/statistics.cpp | 29 +++++++++++++++++------------
1 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/src/statistics.cpp b/src/statistics.cpp
index a39e33c..579ee9c 100644
--- a/src/statistics.cpp
+++ b/src/statistics.cpp
@@ -81,13 +81,12 @@ float Statistics::getUnitFactor(dataUnit unit, long long
value)
{
case humanReadableBit:
case humanReadableByte:
- factor *= 1024 * 1024 * 1024;
- for(int i = 3; i >= 0; --i)
+ for(int i = 0; i < 3; i++)
{
- if(value * (unit % 2 == 0 ? 8 : 1) >= factor)
+ if( value / factor < 500.0 )
return factor;
- factor /= 1024;
+ factor *= 1024.0;
}
return factor;
case bit:
@@ -114,14 +113,20 @@ string Statistics::getUnitString(dataUnit unit, long long
value)
switch(unit)
{
case humanReadableBit:
- case humanReadableByte:
- if(value >= 1024 * 1024 * 1024 / (unit % 2 == 0 ? 8 : 1))
- return 'G' + description;
- if(value >= 1024 * 1024 / (unit % 2 == 0 ? 8 : 1))
- return 'M' + description;
- if(value >= 1024 / (unit % 2 == 0 ? 8 : 1))
- return 'k' + description;
- return description;
+ case humanReadableByte:
+ {
+ const string units[4] = { "", "k", "M", "G" };
+
+ value *= (unit % 2 == 0 ? 8 : 1);
+ for( int i=0; i<4; i++ ){
+ if(value < 500 )
+ return units[i] + description;
+
+ value /= 1024;
+ }
+
+ return units[3] + description;
+ }
case bit:
case byte:
return description;
--
1.7.1
--
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]