Iphone NSNumberFormatter numberFromString返回null

Iphone NSNumberFormatter numberFromString返回null,iphone,objective-c,ios,nsnumber,nsnumberformatter,Iphone,Objective C,Ios,Nsnumber,Nsnumberformatter,这是我的密码 NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; NSNumber *amount = [[NSNumber

这是我的密码

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 转换后的数字为(空) NSN编号为(空) 格式化版本为(空)

我错过什么了吗

编辑:更新代码

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。始终使用除
dealloc
init
之外的访问器

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

[NSString stringWithFormat:@"%@", price]
如果
price
是一个
NSNumber
,那么您可以直接使用它

您正在此处创建一个
NSNumber
,将其分配给
金额
,然后立即将其丢弃。然后,您将超额释放
金额
。因此,您应该预期上述代码会崩溃。(由于
NSNumber
对象的管理方式有点奇怪,下次为整数5创建
NSNumber
时将发生此崩溃。)

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


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

当你持有现金时,你应该总是告诉我们小数点。否则会出现二进制/十进制舍入错误

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

在的第13章的示例代码中提供了一个更完整的管理本地化货币的类(
RNMoney
)。

如果价格的定义类似于此:
NSString*price=@“5”。也不要分配和初始化
金额
,因为您稍后会将其分配给自动释放的值,也不要释放金额。哇。。。所以我所要做的就是格式化价格字符串,使其在开始时包含$a,这样就可以了。。。我在代码中的全部目标是将字符串(示例1.25)转换为货币($1.25)。有更好的方法吗?