【问题标题】:NSNumberFormatter numberFromString returns nullNSNumberFormatter numberFromString 返回 null
【发布时间】:2011-12-06 15:13:00
【问题描述】:

这是我的代码

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 转换后的数字为 (null) 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];

【问题讨论】:

  • 如果价格的定义类似于:NSString *price = @"5";,应该可以工作。也不要分配和初始化amount,因为你稍后将它分配给一个自动释放的值,也不要释放数量。

标签: iphone objective-c ios nsnumber nsnumberformatter


【解决方案1】:

price 是什么?假设它是 ivar,请不要直接访问 ivar。始终使用除deallocinit 之外的访问器。

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

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

如果priceNSNumber,那么你可以直接使用它。

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

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


如果您真的想将字符串转换为 US$,那么可以这样做。请注意,语言环境在这里很重要。如果您使用默认语言环境,那么在法国,“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) 的更完整的类。

【讨论】:

  • 哇...所以我所要做的就是格式化价格字符串以在开头包含 $ 并且它工作正常...我在代码中的全部目标是转换字符串(例如1.25) 转换成货币 ($1.25) 有没有更好的方法来做到这一点?
猜你喜欢
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
相关资源
最近更新 更多