NSNumberFormatter numberFromString 返回 null
这是我的代码
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是
价格
?假设它是一个 ivar,不要直接访问 ivars。始终使用除dealloc
和init
之外的访问器。假设
price
是一个字符串,为什么要这样做:如果
price
是一个NSNumber
,那么你可以直接使用它。您在这里创建一个
NSNumber
,将其分配给amount
,然后立即将其丢弃。然后您过度释放数量
。所以你应该预料到上面的代码会崩溃。 (由于NSNumber
对象的管理方式存在怪癖,下次为整数 5 创建NSNumber
时,就会发生此崩溃。)您的实际问题,金额为
nil
的原因是因为“5”不是当前的货币格式,因此数字格式化程序拒绝了它。如果您在美国并将price
设置为“$5.00”,那么它就可以了。如果您确实想将字符串转换为美元,那么可以这样做。请注意,区域设置在这里很重要。如果您使用默认区域设置,那么在法国“1.25”将是 €1.25,这与 $1.25 不同。
持有货币时,您应该始终使用
NSDecimalNumber
。否则,您可能会遇到二进制/十进制舍入错误。下面使用ARC。
RNMoney) 的更完整的类。 ,descCd-DOWNLOAD.html" rel="noreferrer">iOS 5 编程突破极限。
What is
price
? Assuming it's an ivar, do not access ivars directly. Always use accessors except indealloc
andinit
.Assuming
price
is a string, why are you doing this:If
price
is anNSNumber
, then you can use it directly.You're creating an
NSNumber
here, assigning it toamount
, and then immediately throwing it away. You then over-releaseamount
. So you should expect the above code to crash. (Because of a quirk in howNSNumber
objects are managed, this crash will happen the next time you create anNSNumber
for the integer 5.)And getting all the way around to your actual question, the reason amount is
nil
is because "5" is not in the current currency format, so the number formatter rejected it. If you are in the US and setprice
to "$5.00" then it would work.If you're really trying to convert a string to US$, then this is how to do it. Note that locale matters here. If you use the default locale, then in France "1.25" will be €1.25, which is not the same as $1.25.
You should always us
NSDecimalNumber
when holding currancy. Otherwise you're subject to binary/decimal rounding errors.The following uses ARC.
A more complete class for managing localized currency (
RNMoney
) is available in the example code for chapter 13 of iOS 5 Programming Pushing the Limits.