Changeset 11646


Ignore:
Timestamp:
Jan 8, 2011, 3:16:13 AM (11 years ago)
Author:
livings124
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/NSStringAdditions.m

    r11617 r11646  
    5858+ (NSString *) stringForFileSize: (uint64_t) size
    5959{
    60     const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
     60    const double baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
    6161    const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
    6262   
    63     if (size < baseInt)
    64     {
    65         if (size != 1)
    66             return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
    67         else
    68             return NSLocalizedString(@"1 byte", "File size - bytes");
    69     }
    70 
    71     CGFloat convertedSize;
     63    double convertedSize;
    7264    NSString * unit;
     65    NSUInteger decimals;
    7366    if (size < pow(baseInt, 2))
    7467    {
    7568        convertedSize = size / baseFloat;
    7669        unit = NSLocalizedString(@"KB", "File size - kilobytes");
     70        decimals = 0;
    7771    }
    7872    else if (size < pow(baseInt, 3))
     
    8074        convertedSize = size / powf(baseFloat, 2);
    8175        unit = NSLocalizedString(@"MB", "File size - megabytes");
     76        decimals = 1;
    8277    }
    8378    else if (size < pow(baseInt, 4))
     
    8580        convertedSize = size / powf(baseFloat, 3);
    8681        unit = NSLocalizedString(@"GB", "File size - gigabytes");
     82        decimals = 2;
    8783    }
    8884    else
     
    9086        convertedSize = size / powf(baseFloat, 4);
    9187        unit = NSLocalizedString(@"TB", "File size - terabytes");
    92     }
    93    
    94     //attempt to have minimum of 3 digits with at least 1 decimal
    95     return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
    96                                 : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
     88        decimals = 3; //guessing on this one
     89    }
     90   
     91    //match Finder's behavior
     92    NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
     93    [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
     94    [numberFormatter setMinimumFractionDigits: 0];
     95    [numberFormatter setMaximumFractionDigits: decimals];
     96   
     97    return [[numberFormatter stringFromNumber: [NSNumber numberWithDouble: convertedSize]] stringByAppendingFormat: @" %@", unit];
    9798}
    9899
Note: See TracChangeset for help on using the changeset viewer.