1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00

#3877 Match Finder's file size display: start at KB, each increase in units increases the number of decimals shown, and truncate trailing zeros

This commit is contained in:
Mitchell Livingston 2011-01-08 03:16:13 +00:00
parent 5b2948cd17
commit 035b67677b

View file

@ -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