22

我试图将“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。

你知道我该怎么处理吗?

4

5 回答 5

47

我建议不要对分隔符进行硬编码,以确保基于 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方法。祝你好运!

于 2013-06-23T15:39:58.063 回答
28

所以这个问题是有效的:我自己回答:)

我有一个从 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

问题解决了。

于 2013-04-21T15:04:20.570 回答
7

我最近发现的最简单的解决方案是:

NSString *localizedString = [NSString localizedStringWithFormat:@"%@", @1234567];

也适用于整数:

NSString *localizedString = [NSString localizedStringWithFormat:@"%d", 1234567];

在 Xcode 6 Playground 中验证。但是文档说这个功能一直存在。

于 2014-08-22T19:31:33.943 回答
5
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.usesGroupingSeparator = YES;
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
NSString *formattedNumberString = [numberFormatter stringFromNumber:@122344.00];`

如果您正在处理小数点,这将解决您的四舍五入问题。

于 2014-07-09T10:07:40.130 回答
3

对于货币使用...

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"

正如人们所期望的那样

于 2017-12-12T22:12:19.420 回答