我有以下内容:
NSLog(@"Total: $%.2f", paymentsToday);
它当前显示:
$16000.00
如何让它显示:
$16,000.00
我有以下内容:
NSLog(@"Total: $%.2f", paymentsToday);
它当前显示:
$16000.00
如何让它显示:
$16,000.00
您应该为此使用NSNumberFormatter 。
这应该可以解决问题:
NSNumberFormatter *priceFormatter = [[NSNumberFormatter alloc] init];
[priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[priceFormatter setCurrencyCode:@"USD"];
[priceFormatter setCurrencySymbol:@"$"];
NSString *formattedOutput = [priceFormatter stringFromNumber:[NSNumber numberWithFloat: paymentsToday]];
NSLog(@"Total: %@", formattedOutput);
使用 NSNumberFormatter 设置货币格式:
NSNumberFormatter* formatter=[[NSNumberFormatter alloc]init];
[formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSLog(@"%@",[formatter stringFromNumber: @(paymentsToday)]);