Xcode Iphone应用程序:如何打印价格浮动

时间:2012-10-05 07:34:57

标签: iphone objective-c xcode

  

可能重复:
  Objective-C: How to format string as $ Price

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

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

因此,大值显示为:$ 12123041

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

我该怎么做?

感谢。感谢您的帮助 添

2 个答案:

答案 0 :(得分:1)

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

[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *num = [NSNumber numberWithInt:tempPrice];

NSString *str = [formatter stringFromNumber:num];

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

答案 1 :(得分: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