如何在iOS中使用逗号分隔显示数字值

时间:2013-12-05 08:57:18

标签: ios objective-c

数字值为120000。

  1. 列出项目    这个数值我需要显示1,20,000。

3 个答案:

答案 0 :(得分:6)

您可以使用NSNumberFormatterCurrencyStyle获得所需的结果。

NSNumberFormatter *indCurrencyFormatter = [[NSNumberFormatter alloc] init];
[indCurrencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[indCurrencyFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"]];
NSString *formattedString =  [indCurrencyFormatter stringFromNumber:[NSNumber numberWithFloat:120000]];

答案 1 :(得分:1)

使用NSNumberFormatter

示例:

NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // Here you can choose the style

NSString *formatted = [numberFormatter stringFromNumber:[NSNumber numberWithInteger: 120000]];

或者您可以使用:  [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]

<强>更新

在印度货币格式中,您必须将@"en_IN"设置为numberFormatter Locale,只需添加以下行:

  [numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"]]; 

答案 2 :(得分:1)

NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"120000"];
NSLocale *priceLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-in"]; 

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setLocale:priceLocale];
NSString *format = [currencyFormatter positiveFormat];
[currencyFormatter setPositiveFormat:format];

NSString *currencyString1 = [currencyFormatter stringFromNumber:price];
DLog(@"%@",currencyString1);