Here\'s my code
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
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.