views:

279

answers:

2

My iPhone app formats an NSDecimalNumber as a currency using setCurrencyCode, however another screen displays just the currency symbol. Rather than storing both the currency code and symbol, is it possible to derive the symbol from the code? I thought the following might work, but it just returns the symbol as $:

currencyCode = [dictPrices valueForKey:@"currencyCode"];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setCurrencyCode:currencyCode];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSString *currencySymbol = [numberFormatter currencySymbol];
+2  A: 

You want to be setting the locale, not the currency code. Then you will get the expected results from the formatters properties (or tweak them). You can also get things like the currency symbol from the locale object itself. So, for example, if you were displaying a number formatted for Japan in JPY:

NSLocale* japanese_japan = [[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"] autorelease];
NSNumberFormatter fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmtr setLocale:japanese_japan];

Now your number formatter should give you the correct symbols for currency symbol and format according to the rules in the specified locale.

Jason Coco
Excellent, thanks!
Dave Hunt
A: 

Given this answer, is there an easy way to identify the locale from the phone? So that the first line would be NSLocale* from_phone = [[[NSLocale alloc] initWithLocalIdentifier:@"SOME VALUE FROMTHE PHONE"] autorelease]; ?

Michael Rowe
Try `[NSLocale systemLocale]`
glenc