iphone - NSNumberFormatter numberFromString 返回 null

标签 iphone objective-c ios nsnumber nsnumberformatter

这是我的代码

NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];

    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];

    NSNumber *amount = [[NSNumber alloc] init];

    NSLog(@"the price string is %@", price);

    amount = [currencyStyle numberFromString:price];

    NSLog(@"The converted number is %@",[currencyStyle numberFromString:price]);
    NSLog(@"The NSNumber is %@", amount);

    NSLog(@"The formatted version is %@", [currencyStyle stringFromNumber:amount]);

    NSLog(@"--------------------");
    self.priceLabel.text = [currencyStyle stringFromNumber:amount]; 

    [amount release];
    [currencyStyle release];

这是日志吐出的内容

价格串是5 转换后的数字是(空) NSNumber 是(空) 格式化版本为(null)

我错过了什么吗?

编辑:更新代码

NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];

    NSNumber *amount = [currencyStyle numberFromString:price];

    NSLog(@"the price string is %@", price);
    NSLog(@"The converted number is %@",[currencyStyle numberFromString:price]);
    NSLog(@"The NSNumber is %@", amount);
    NSLog(@"The formatted version is %@", [currencyStyle stringFromNumber:amount]);
    NSLog(@"--------------------");

    self.priceLabel.text = [NSString stringWithFormat:@" %@ ", [currencyStyle stringFromNumber:amount]]; 

    [currencyStyle release];

最佳答案

什么是价格?假设它是一个 ivar,请不要直接访问 ivar。始终使用访问器,deallocinit 除外。

假设 price 是一个字符串,你为什么要这样做:

[NSString stringWithFormat:@"%@", price]

如果price是一个NSNumber,那么你可以直接使用它。

您在这里创建了一个 NSNumber,将其分配给 amount,然后立即将其丢弃。然后,您过度释放 amount。所以你应该预料到上面的代码会崩溃。 (由于 NSNumber 对象的管理方式有一个怪癖,下次您为整数 5 创建 NSNumber 时会发生此崩溃。)

一直到你的实际问题,金额为 nil 的原因是因为“5”不是当前的货币格式,所以数字格式化程序拒绝了它。如果您在美国并将 price 设置为“$5.00”,那么它会起作用。


如果您真的想将一个字符串转换为美元,那么这就是实现它的方法。请注意,语言环境在这里很重要。如果您使用默认区域设置,那么在法国“1.25”将是 1.25 欧元,这与 1.25 美元不同。

持有货币时,您应该始终使用 NSDecimalNumber。否则,您会遇到二进制/十进制舍入错误。

以下使用 ARC。

NSString *amountString = @"5.25";
NSDecimalNumber *amountNumber = [NSDecimalNumber decimalNumberWithString:amountString];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];    
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyStyle setLocale:locale];
NSString *currency = [currencyStyle stringFromNumber:amountNumber];

NSLog(@"%@", currency);

iOS 5 Programming Pushing the Limits 的第 13 章的示例代码中提供了用于管理本地化货币 (RNMoney) 的更完整的类。 .

关于iphone - NSNumberFormatter numberFromString 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8402132/

相关文章:

iphone - iPhone 视频通话/ session API

iphone - 如何为MKMapView创建触摸事件?

iphone - 编辑时更改 uitextfield 返回键类型

iphone - 如何设置 MGTwitterEngine

objective-c - 在文档加载时执行 Objective-C 方法

objective-c - 枚举ChildNodesWithName :usingBlock: search expressions not functioning

ios - 更改 UIBarButtonItem 的 Tint 颜色

ios - Swift NotificationCenter 将 userInfo 传递给观察 View Controller

iOS:geojson iOS 访问

ios - 找不到 NSAlert 类型