Changeset 167


Ignore:
Timestamp:
Mar 28, 2006, 10:36:56 AM (17 years ago)
Author:
titer
Message:

Makes stringForFileSize: always show 3 significant digits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/StringAdditions.m

    r162 r167  
    3434{
    3535    if (size < 1024)
     36    {
     37        /* 0 to 1023 bytes */
    3638        return [NSString stringWithFormat: @"%lld bytes", size];
    37     else if (size < 1048576)
    38         return [NSString stringWithFormat: @"%lld.%lld KB",
    39                 size / 1024, ( size % 1024 ) / 103];
     39    }
     40
     41    NSString * unit;
     42    if (size < 1048576)
     43    {
     44        unit = @" KB";
     45    }
    4046    else if (size < 1073741824)
    41         return [NSString stringWithFormat: @"%lld.%lld MB",
    42                 size / 1048576, ( size % 1048576 ) / 104858];
     47    {
     48        unit = @" MB";
     49        size /= 1024;
     50    }
    4351    else
    44         return [NSString stringWithFormat: @"%lld.%lld GB",
    45                 size / 1073741824, ( size % 1073741824 ) / 107374183];
     52    {
     53        unit = @" GB";
     54        size /= 1048576;
     55    }
     56
     57    NSString * value;
     58    if (size < 10240)
     59        /* 1.00 to 9.99 XB */
     60        value = [NSString stringWithFormat: @"%lld.%02lld",
     61                    size / 1024, ( size % 1024 ) * 100 / 1024];
     62    else if (size < 102400)
     63        /* 10.0 to 99.9 XB */
     64        value = [NSString stringWithFormat: @"%lld.%lld KB",
     65                    size / 1024, ( size % 1024 ) * 10 / 1024];
     66    else
     67        /* 100+ XB */
     68        value = [NSString stringWithFormat: @"%lld", size / 1024];
     69
     70    return [value stringByAppendingString: unit];
    4671}
    4772
Note: See TracChangeset for help on using the changeset viewer.