NSNumberFormatter numberFromString 返回 null

发布于 2024-12-20 04:49:22 字数 1593 浏览 2 评论 0 原文

这是我的代码

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];

Here's my code

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];

This is what the log spits out

the price string is 5
The converted number is (null)
The NSNumber is (null)
The formatted version is (null)

Am I missing something?

EDIT: Updated code

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2024-12-27 04:49:22

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

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

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

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

您在这里创建一个 NSNumber,将其分配给 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);

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 in dealloc and init.

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

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

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

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.)

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.


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.

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);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文