NSNumberFormatter numberFromString返回null [英] NSNumberFormatter numberFromString returns null

查看:131
本文介绍了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
转换后的数字是(null)
NSNumber是(null)
格式化版本是(null)

the price string is 5 The converted number is (null) The NSNumber is (null) The formatted version is (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,不要直接访问ivars。始终使用访问者,除了 dealloc init

What is price? Assuming it's an ivar, do not access ivars directly. Always use accessors except in dealloc and init.

假设价格是一个字符串,为什么这样做:

Assuming price is a string, why are you doing this:

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

如果价格 NSNumber ,然后你可以直接使用它。

If price is an NSNumber, then you can use it directly.

你正在创建一个 NSNumber 此处,将其分配给金额,然后立即将其丢弃。然后你过度释放金额。所以你应该期望上面的代码崩溃。 (由于管理 NSNumber 对象的怪癖,下次创建 NSNumber 时会发生此崩溃整数5。)

You're creating an NSNumber here, assigning it to amount, and then immediately throwing it away. You then over-release amount. So you should expect the above code to crash. (Because of a quirk in how NSNumber objects are managed, this crash will happen the next time you create an NSNumber for the integer 5.)

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

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 set price to "$5.00" then it would work.

如果你真的想把一个字符串转换成美元,那么这就是怎么做的。请注意,语言环境很重要。如果你使用默认的语言环境,那么在法国1.25将是1.25欧元,这与1.25美元不同。

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.

你应该总是我们 NSDecimalNumber 保持优雅。否则,您将受到二进制/十进制舍入错误的影响。

You should always us NSDecimalNumber when holding currancy. Otherwise you're subject to binary/decimal rounding errors.

以下使用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);

管理本地化货币的更完整类( RNMoney )可在 iOS 5的第13章的示例代码中找到编程推动限制

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.

这篇关于NSNumberFormatter numberFromString返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆