]> granicus.if.org Git - transmission/commitdiff
#3877 Match Finder's file size display: start at KB, each increase in units increases...
authorMitchell Livingston <livings124@transmissionbt.com>
Sat, 8 Jan 2011 03:16:13 +0000 (03:16 +0000)
committerMitchell Livingston <livings124@transmissionbt.com>
Sat, 8 Jan 2011 03:16:13 +0000 (03:16 +0000)
macosx/NSStringAdditions.m

index be7cd1c7beac5ae1bcc743437d92734e6393bc08..d1667bed240b96e5b886ce10a7a70e3b37ea51e1 100644 (file)
 
 + (NSString *) stringForFileSize: (uint64_t) size
 {
-    const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
+    const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
     const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
     
-    if (size < baseInt)
-    {
-        if (size != 1)
-            return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
-        else
-            return NSLocalizedString(@"1 byte", "File size - bytes");
-    }
-
-    CGFloat convertedSize;
+    double convertedSize;
     NSString * unit;
+    NSUInteger decimals;
     if (size < pow(baseInt, 2))
     {
         convertedSize = size / baseFloat;
         unit = NSLocalizedString(@"KB", "File size - kilobytes");
+        decimals = 0;
     }
     else if (size < pow(baseInt, 3))
     {
         convertedSize = size / powf(baseFloat, 2);
         unit = NSLocalizedString(@"MB", "File size - megabytes");
+        decimals = 1;
     }
     else if (size < pow(baseInt, 4))
     {
         convertedSize = size / powf(baseFloat, 3);
         unit = NSLocalizedString(@"GB", "File size - gigabytes");
+        decimals = 2;
     }
     else
     {
         convertedSize = size / powf(baseFloat, 4);
         unit = NSLocalizedString(@"TB", "File size - terabytes");
+        decimals = 3; //guessing on this one
     }
     
-    //attempt to have minimum of 3 digits with at least 1 decimal
-    return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
-                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
+    //match Finder's behavior
+    NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
+    [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
+    [numberFormatter setMinimumFractionDigits: 0];
+    [numberFormatter setMaximumFractionDigits: decimals];
+    
+    return [[numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]] stringByAppendingFormat: @" %@", unit];
 }
 
 + (NSString *) stringForSpeed: (CGFloat) speed