これが私のコードです
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) です
何か不足していますか?
編集:更新されたコード
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];