Changeset 422


Ignore:
Timestamp:
Jun 21, 2006, 2:49:53 PM (16 years ago)
Author:
livings124
Message:

String formatting is simpler.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/StringAdditions.m

    r421 r422  
    4545{
    4646    if (size < 1024)
    47     {
    48         /* 0 to 1023 bytes */
    4947        return [NSString stringWithFormat: @"%lld bytes", size];
    50     }
    5148
     49    float displaySize = (float) size / 1024.0;
    5250    NSString * unit;
    5351    if (size < 1048576)
    54     {
    5552        unit = @" KB";
    56     }
    5753    else if (size < 1073741824)
    5854    {
     55        displaySize /= 1024.0;
    5956        unit = @" MB";
    60         size /= 1024;
    6157    }
    6258    else
    6359    {
     60        displaySize /= 1048576.0;
    6461        unit = @" GB";
    65         size /= 1048576;
    6662    }
    6763
    6864    NSString * value;
    69     if (size < 10240)
    70         /* 1.00 to 9.99 XB */
    71         value = [NSString stringWithFormat: @"%lld.%02lld", size / 1024, ( size % 1024 ) * 100 / 1024];
    72     else if (size < 102400)
    73         /* 10.0 to 99.9 XB */
    74         value = [NSString stringWithFormat: @"%lld.%lld", size / 1024, ( size % 1024 ) * 10 / 1024];
     65    if (displaySize < 10.0)
     66        value = [NSString stringWithFormat: @"%.2f", displaySize];
     67    else if (displaySize < 100.0)
     68        value = [NSString stringWithFormat: @"%.1f", displaySize];
    7569    else
    76         /* 100+ XB */
    77         value = [NSString stringWithFormat: @"%lld", size / 1024];
     70        value = [NSString stringWithFormat: @"%.0f", displaySize];
    7871
    7972    return [value stringByAppendingString: unit];
     
    8780+ (NSString *) stringForSpeedAbbrev: (float) speed
    8881{
    89     if (speed < 1000)         /* 0.0 K to 999.9 K */
     82    if (speed < 1000.0) //0.0 K to 999.9 K
    9083        return [NSString stringWithFormat: @"%.1f K", speed];
    91     else if (speed < 102400)  /* 0.98 M to 99.99 M */
    92         return [NSString stringWithFormat: @"%.2f M", speed / 1024];
    93     else if (speed < 1024000) /* 100.0 M to 999.9 M */
    94         return [NSString stringWithFormat: @"%.1f M", speed / 1024];
    95     else                      /* Insane speeds */
    96         return [NSString stringWithFormat: @"%.2f G", speed / 1048576];
     84    else if (speed < 102400.0) //0.98 M to 99.99 M
     85        return [NSString stringWithFormat: @"%.2f M", speed / 1024.0];
     86    else if (speed < 1024000.0) //100.0 M to 999.9 M
     87        return [NSString stringWithFormat: @"%.1f M", speed / 1024.0];
     88    else //insane speeds
     89        return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0];
    9790}
    9891
     
    10396
    10497    float ratio = (float) up / (float) down;
    105     if( ratio < 10.0 )
     98    if (ratio < 10.0)
    10699        return [NSString stringWithFormat: @"%.2f", ratio];
    107     else if( ratio < 100.0 )
     100    else if (ratio < 100.0)
    108101        return [NSString stringWithFormat: @"%.1f", ratio];
    109102    else
Note: See TracChangeset for help on using the changeset viewer.