]> granicus.if.org Git - transmission/commitdiff
cleaner code for rounding file size, speed, and ratio
authorMitchell Livingston <livings124@transmissionbt.com>
Mon, 7 Apr 2008 02:17:45 +0000 (02:17 +0000)
committerMitchell Livingston <livings124@transmissionbt.com>
Mon, 7 Apr 2008 02:17:45 +0000 (02:17 +0000)
macosx/NSStringAdditions.m

index 152b1b21046d62b039174085cb78da2a06d9291c..ee886137132e3e7f1c072c72de5317aac9307424 100644 (file)
@@ -25,8 +25,6 @@
 #import "NSStringAdditions.h"
 #import <transmission.h>
 
-#warning better rounding
-
 @implementation NSString (NSStringAdditions)
 
 + (NSString *) ellipsis
@@ -68,8 +66,8 @@
     }
     
     //attempt to have minimum of 3 digits with at least 1 decimal
-    return convertedSize < 10.0 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize - .005, unit]
-                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize - .05, unit];
+    return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
+                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
 }
 
 + (NSString *) stringForSpeed: (float) speed
 
 + (NSString *) stringForSpeedAbbrev: (float) speed
 {
-    if (speed < 1000.0) //0.0 K to 999.9 K
-        return [NSString localizedStringWithFormat: @"%.1f K", MAX(0.0, speed - .05)];
-    else if (speed < 102400.0) //0.98 M to 99.99 M
-        return [NSString localizedStringWithFormat: @"%.2f M", (speed / 1024.0) - .005];
-    else if (speed < 1024000.0) //100.0 M to 999.9 M
-        return [NSString localizedStringWithFormat: @"%.1f M", (speed / 1024.0) - .05];
+    if (speed <= 999.95) //0.0 K to 999.9 K
+        return [NSString localizedStringWithFormat: @"%.1f K", speed];
+    
+    speed /= 1024.0;
+    
+    if (speed <= 99.995) //0.98 M to 99.99 M
+        return [NSString localizedStringWithFormat: @"%.2f M", speed];
+    else if (speed <= 999.95) //100.0 M to 999.9 M
+        return [NSString localizedStringWithFormat: @"%.1f M", speed];
     else //insane speeds
-        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1048576.0) - .005];
+        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0)];
 }
 
 + (NSString *) stringForRatio: (float) ratio
         return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
     else;
     
-    if (ratio < 10.0)
-        return [NSString localizedStringWithFormat: @"%.2f", MAX(0.0, ratio - .005)];
-    else if (ratio < 100.0)
-        return [NSString localizedStringWithFormat: @"%.1f", ratio - .05];
-    else
-        return [NSString localizedStringWithFormat: @"%.0f", ratio];
+    if (ratio <= 9.995) //0.00 to 9.99
+        return [NSString localizedStringWithFormat: @"%.2f", ratio];
+    else if (ratio <= 99.95) //10.0 to 99.9
+        return [NSString localizedStringWithFormat: @"%.1f", ratio];
+    else //rest are single digit
+        return [NSString stringWithFormat: @"%d", ratio];
 }
 
 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds