2

可能重复:
Objective-C:如何将字符串格式化为 $ Price

我有一个显示价格的应用程序。价格以这种方式显示:

[self makePrice];
itemPrice.text      = [NSString stringWithFormat:@"%i",tempPrice];
itemPrice.textColor = [UIColor whiteColor];

结果大值显示为:$ 12123041

我需要它是 12,123,041 美元

我该怎么做?

谢谢。感谢您的帮助蒂姆

4

2 回答 2

1
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *num = [NSNumber numberWithInt:tempPrice];

NSString *str = [formatter stringFromNumber:num];

itemPrice.text = [NSString stringWithFormat:@"%@",str];
于 2012-10-05T07:46:55.610 回答
1

嗨,你可以这样做: -

int tempPrice=123456;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *yourNumber = [NSNumber numberWithInt:tempPrice];
NSString *str = [formatter stringFromNumber:yourNumber];

str = [NSString stringWithFormat:@"%@",str];

NSLog(@"my corrunsy === %@",str);

输出是

hiiii === $123,456.00

于 2012-10-05T08:13:42.783 回答