NSNumberFormatter setFormat错误

时间:2013-11-20 05:53:28

标签: ios

使用我的第一个iOS应用。我认为这段代码应该可行,但是我收到以下错误。也许我错过了一个导入或什么?

No visible @interface for 'NSNumberFormatter' declares the selector'setFormat'

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setFormat:@"#0.00"];

2 个答案:

答案 0 :(得分:1)

您无法设置此处提供的格式。设置数字格式如下所示

[formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
//or other styles like NSNumberFormatterDecimalStyle,NSNumberFormatterPercentStyle

或者您可以创建自定义数字格式

[formatter setPositiveFormat:@"#0.00"];

这里给出了一个很好的教程

http://useyourloaf.com/blog/2012/06/14/using-number-formatters.html

答案 1 :(得分:1)

你不能为NSNumberFormatter调用setFormat:。

一些示例代码:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]];
NSLog(@"Formatted number string:%@",string);

numberStyle枚举:

enum {  
    NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
    NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
    NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
    NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
    NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
    NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
};

为每个numberStyle重新输出:

[1243:403] Formatted number string:123456789
[1243:403] Formatted number string:123,456,789
[1243:403] Formatted number string:¥123,456,789.00
[1243:403] Formatted number string:-539,222,988%
[1243:403] Formatted number string:1.23456789E8
[1243:403] Formatted number string:one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine