我尝试将像“12000.54”这样的NSString转换为“12.000,54”。我写了一个NSNumberFormatter实例。
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@"."];
[formatter setDecimalSeparator:@","];
但是当我NSLog这个:
NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]);
它打印空值。如果我用一个点改变我的逗号它正常工作。我只是希望能够把我想要的东西放在我的分隔符字段(逗号,点,斜线等等)中,我有不同的输出:12 / 000,54 12.000.54 12,000,54例如。
你知道我怎么处理这个吗?
答案 0 :(得分:41)
我建议不要对分隔符进行硬编码,以确保基于iPhone区域设置的正确分隔符行为。最简单的方法是:
使用objective-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
使用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
我不会使用var-name“numberFromString”,因为它是NSNumberFormatter
方法。
祝你好运!
答案 1 :(得分:26)
所以问题是有效的:我自己回答:)
我从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
问题解决了。
答案 2 :(得分:6)
我最近发现的最简单的解决方案是:
NSString *localizedString = [NSString localizedStringWithFormat:@"%@", @1234567];
也适用于整数:
NSString *localizedString = [NSString localizedStringWithFormat:@"%d", 1234567];
在Xcode 6游乐场验证。但是医生说这个功能一直存在。
答案 3 :(得分:3)
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.usesGroupingSeparator = YES;
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
NSString *formattedNumberString = [numberFormatter stringFromNumber:@122344.00];`
如果您处理小数点,这将解决您的舍入问题。
答案 4 :(得分:2)
货币用途......
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
使用示例:
let value: Double = 12345.6789
numberFormatter.string(from: value as NSNumber)
...产量
"$ 12,345.68"
正如人们所期望的那样