用户将输入美元值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);
答案 0 :(得分: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);
答案 1 :(得分: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;
}