objective-c 如何在Objective C中每3位数字添加逗号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2233824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add commas to number every 3 digits in Objective C?
提问by RexOnRoids
If I have a number int aNum = 2000000how do I format this so that I can display it as the NSString 2,000,000?
如果我有一个数字int aNum = 2000000,我该如何格式化它以便我可以将它显示为 NSString 2,000,000?
回答by Nik
Use NSNumberFormatter.
使用NSNumberFormatter.
Specifically:
具体来说:
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!
NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:2000000]];
[formatter release];
By default NSNumberFormatteruses the current locale so the grouping separators are set to their correct values by default. The key thing is to remember to set a number style.
默认情况下NSNumberFormatter使用当前语言环境,因此分组分隔符默认设置为正确的值。关键是要记住设置数字样式。
回答by Barry Wark
Don't do your own number formatting. You will almost certainly not get all the edge cases right or correctly handle all possible locales. Use the NSNumberFormatterfor formatting numeric data to a localized string representation.
不要做你自己的数字格式。您几乎肯定不会正确处理所有边缘情况或正确处理所有可能的语言环境。使用 将NSNumberFormatter数字数据格式化为本地化的字符串表示形式。
You would use the NSNumberFormatterinstance method -setGroupingSeparator:to set the grouping separator to @","(or better yet [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]; thanks @ntesler) and -setGroupingSize:to put a grouping separator every 3 digits.
您将使用NSNumberFormatter实例方法-setGroupingSeparator:将分组分隔符设置为@","(或更好[[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];感谢@ntesler)并-setGroupingSize:每 3 位设置一个分组分隔符。
回答by Phil Calvin
There's a static method on NSNumberFormatterthat does just what you need:
有一个静态方法NSNumberFormatter可以满足您的需求:
int aNum = 2000000;
NSString *display = [NSNumberFormatter localizedStringFromNumber:@(aNum)
numberStyle:NSNumberFormatterDecimalStyle];
This way is a little more succinct than creating a new NSNumberFormatterif you don't need to do any additional configuration of the formatter.
NSNumberFormatter如果您不需要对格式化程序进行任何额外的配置,那么这种方式比创建一个新的方式更简洁一些。
回答by yujean
Even easier:
更简单:
NSNumber *someNumber = @(1234567890);
NSString *modelNumberString = [NSString localizedStringWithFormat:@"%@", someNumber];
NSLog(@"Number with commas: %@", modelNumberString);
coworker just taught me this today. #amazing
今天同事刚教我这个。#惊人的
回答by Nazir
Think some as i will get this post looking for sample. So if you are working with number make attention on next params:
想想一些,因为我会得到这篇文章寻找样本。因此,如果您正在使用数字,请注意下一个参数:
setNumberStyle:NSNumberFormatterCurrencyStyle // if you are working with currency
It could be also
也可以
setNumberStyle:NSNumberFormatterDecimalStyle
All code is For ARC.
所有代码都是为了 ARC。
If you are working with Integer and need to get result such as 200,000
如果您正在使用 Integer 并且需要获得诸如 200,000 之类的结果
int value = 200000;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString * newString = [formatter stringFromNumber:[NSNumber numberWithInteger:value]];
If you are working with Float and need to get result such as 200,000.00
如果您正在使用 Float 并且需要获得诸如 200,000.00 之类的结果
float value = 200000;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
NSString * newString = [formatter stringFromNumber:[NSNumber numberWithFloat:value]];
EDIT
编辑
To have ability to use different digital separators use NSLocale.
Add to code where NSLocaleis specified on Locale Identifier:
要能够使用不同的数字分隔符,请使用NSLocale. 添加到NSLocale区域设置标识符指定的代码:
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"]];
or use current local:
或使用当前本地:
[formatter setLocale:[NSLocale currentLocale]];
回答by Raj iOS
Swift version
迅捷版
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.maximumFractionDigits = decimalPlaces
let result = formatter.stringFromNumber(NSNumber(double: 8.0))
回答by Julfikar
An easy solution could be this. My answer is almost same like @Nazir's answer but with a small trick.
一个简单的解决方案可能是这样。我的答案与@Nazir 的答案几乎相同,但有一个小技巧。
double current_balance = 2000000.00;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
//[formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; //if you want for currency with $ sign
[formatter setMinimumFractionDigits:2]; // Set this if you need 2 digits
[formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
NSString * currency_format = [NSString stringWithFormat:@"%@", [formatter stringFromNumber:[NSNumber numberWithDouble:current_balance]]];
回答by Sandip Patel - SM
For Swift 4.0
对于 Swift 4.0
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
let result = formatter.string(from: NSNumber(value: 123456))

