5

用户将输入一个美元值作为int,我想将结果转换为一个缩短的格式化字符串。因此,如果用户输入 1700,字符串将显示“$1.7k”。如果用户输入 32600000,字符串将显示“$32.6m”。

更新

这是我到目前为止的代码。它似乎适用于大约 10k 的数字。我会为更大的数字添加更多 if 语句。但是有没有更有效的方法来做到这一点?

NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; 
[nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[nformat setCurrencySymbol:@"$"]; 
[nformat setNumberStyle:NSNumberFormatterCurrencyStyle]; 
double doubleValue = 10200; 
NSString *stringValue = nil; 
NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; 

for (NSString *s in abbrevations) 
{ 

    doubleValue /= 1000.0 ; 

    if ( doubleValue < 1000.0 ) 
    { 

        if ( (long long)doubleValue % (long long) 100 == 0 ) { 
            [nformat setMaximumFractionDigits:0]; 
        } else {                 
            [nformat setMaximumFractionDigits:2]; 
        } 

        stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; 
        NSUInteger stringLen = [stringValue length]; 

        if ( [stringValue hasSuffix:@".00"] ) 
        {                
            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];             
        } else if ( [stringValue hasSuffix:@".0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)]; 

        } else if ( [stringValue hasSuffix:@"0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];         
        } 


        // Add the letter suffix at the end of it 
        stringValue = [stringValue stringByAppendingString: s]; 

        //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]]  , s] ; 
        break ; 
    }    
}  

NSLog(@"Cash = %@", stringValue); 
4

2 回答 2

14
unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSString *svalue = [NSString stringWithFormat:@"$%.*f%@",
                    //Use boolean as 0 or 1 for precision
                    (int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0)),
                    dvalue, [suffix objectAtIndex:index]];
NSLog(@"Value: %@", svalue);

ARC 本地化版本

unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//Germany Example
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de-de"]];
//Set fractional digits to 0 or 1
[formatter setMaximumFractionDigits:(int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0))];

NSString *svalue = [[formatter stringFromNumber:[NSNumber numberWithFloat:dvalue]]
                    stringByAppendingString:[suffix objectAtIndex:index]];

NSLog(@"Value: %@", svalue);
于 2012-08-16T20:53:46.637 回答
7

如果你想要一个具有自动调整宽度的字符串,你可以使用:

+(NSString*)numberWithShortcut:(NSNumber*)number
{
    unsigned long long value = [number longLongValue];

    NSUInteger index = 0;
    double dvalue = (double)value;

    NSArray *suffix = @[ @"", @"K", @"M", @"B", @"T", @"P", @"E" ];

    while ((value /= 1000) && ++index) dvalue /= 1000;

    NSString *svalue = [NSString stringWithFormat:@"%@%@",[NSNumber numberWithDouble:dvalue], [suffix objectAtIndex:index]];

    return svalue;
}
于 2013-05-28T21:06:19.347 回答