diff --git a/macosx/NSStringAdditions.m b/macosx/NSStringAdditions.m index be7cd1c7b..d1667bed2 100644 --- a/macosx/NSStringAdditions.m +++ b/macosx/NSStringAdditions.m @@ -57,43 +57,44 @@ + (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