ios 带有逗号小数分隔符的 NSNumberFormatter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16123119/
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
NSNumberFormatter with comma decimal separator
提问by Pierre
I tried to convert an NSString like "12000.54" into "12.000,54". I wrote an NSNumberFormatter instance.
我试图将像“12000.54”这样的 NSString 转换为“12.000,54”。我写了一个 NSNumberFormatter 实例。
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@"."];
[formatter setDecimalSeparator:@","];
But when I NSLog this :
但是当我 NSLog 这个:
NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]);
It prints null value. If I change my comma with a point it's working correctly. I just would like to be able to put what I want for my separators field (comma, point, slash, etc ...) and I have different outputs : 12/000,54 12.000.54 12,000,54 for example.
它打印空值。如果我用一个点更改我的逗号,它就可以正常工作。我只是希望能够为我的分隔符字段(逗号、点、斜线等)输入我想要的内容,并且我有不同的输出:例如 12/000,54 12.000.54 12,000,54。
Do you know how I can handle this ?
你知道我怎么处理吗?
回答by JFS
I would recommend not hardcoding the separator to ensure the right separator behavior based on the iPhone locale setting. The easiest way to to this is:
我建议不要对分隔符进行硬编码,以确保根据 iPhone 语言环境设置正确的分隔符行为。最简单的方法是:
using objective-c
使用目标-c
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behavior
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
numberFormatter.usesGroupingSeparator = YES;
// example for writing the number object into a label
cell.finalValueLabel.text = [NSString StringWithFormat:@"%@", [numberFormatter stringForObjectValue:numberFromString]]; // your var name is not well chosen
using SWIFT 3
使用 SWIFT 3
let formatter = NumberFormatter()
formatter.locale = NSLocale.current // this ensures the right separator behavior
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.usesGroupingSeparator = true
// example for writing the number object into a label
// your variable "numberFromString" needs to be a NSNumber object
finalValueLabel.text = formatter.string(from: numberFromString)! // your var name is not well chosen
and I would not use the var-name "numberFromString" because it is an NSNumberFormatter
method.
Good luck!
我不会使用 var-name "numberFromString" 因为它是一种NSNumberFormatter
方法。祝你好运!
回答by Pierre
So the question is valid : I'm answering it myself :)
所以这个问题是有效的:我自己回答:)
I have a decimal value read from sqlite (e.g 12000 or 12000.54) directly transformed into NSString. I have to use different separator at some point in my code.
我有一个从 sqlite(例如 12000 或 12000.54)读取的十进制值直接转换为 NSString。我必须在代码中的某个时候使用不同的分隔符。
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@""];
[formatter setDecimalSeparator:@"."];
// Decimal values read from any db are always written with no grouping separator and a comma for decimal.
NSNumber *numberFromString = [formatter numberFromString:@"12000.54"]];
[formatter setGroupingSeparator:@" "]; // Whatever you want here
[formatter setDecimalSeparator:@","]; // Whatever you want here
NSString *finalValue = [formatter stringFromNumber:numberFromString];
NSLog(@"%@",finalValue); // Print 12 000,54
Problem solved.
问题解决了。
回答by mxcl
The simplest solution I recently found is:
我最近发现的最简单的解决方案是:
NSString *localizedString = [NSString localizedStringWithFormat:@"%@", @1234567];
Also works with ints:
也适用于整数:
NSString *localizedString = [NSString localizedStringWithFormat:@"%d", 1234567];
Verified in Xcode 6 playground. But docs say this function has always existed.
在 Xcode 6 playground 中验证。但是文档说这个功能一直存在。
回答by Rinku
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.usesGroupingSeparator = YES;
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
NSString *formattedNumberString = [numberFormatter stringFromNumber:@122344.00];`
This wll fix your rounding up problem if you are dealing with decimal points.
如果您正在处理小数点,这将解决您的舍入问题。
回答by Brandon Zacharie
For currency use...
对于货币使用...
let numberFormatter = NumberFormatter()
// Produces symbol (i.e. "$ " for en_US_POSIX) and decimal formatting
numberFormatter.numberStyle = .currency
// Produces commas; i.e. "1,234"
numberFormatter.usesGroupingSeparator = true
numberFormatter.groupingSize = 3
Example usage:
用法示例:
let value: Double = 12345.6789
numberFormatter.string(from: value as NSNumber)
yields...
产生...
"$?12,345.68"
as one would expect
正如人们所期望的那样