NSNumberFormatter currency symbol

kas-kad picture kas-kad · Jul 14, 2014 · Viewed 8.8k times · Source

My application is allowing user to select various currencies for spendings tracking. I have a label which displays the amount with currecy symbol. I'm using NSNumberFormatter with kCFNumberFormatterCurrencyStyle to format the amount string and display it in the label;

numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = kCFNumberFormatterCurrencyStyle;
numberFormatter.currencyCode = @"EUR";

My goal is to display the currency symbol with different color, so i'm using NSAttributedString, trying to find symbols' range and set different attributes to it. The problem is that the formatter return wrong symbol when I initilizing the attributed string:

MLOG(@"internationalCurrencySymbol %@", numberFormatter.internationalCurrencySymbol);
MLOG(@"currencySymbol %@", numberFormatter.currencySymbol);
MLOG(@"currencyCode %@", numberFormatter.currencyCode);

//logs:
//USD
//$
//EUR

but when the label is displayed on the screen I see correct Euro currency symbol:

Does anybody know how can get the currency symbol for given currency code?

Answer

Mundi picture Mundi · Jul 14, 2014

I just tested your code. If you run the log statements right after defining the number formatter the way you did, the output is

@"EUR"
@"€"
@"EUR"

Note that international currency symbol and currency symbol seem to depend on the locale set in your system. But you can easily change the locale for the formatter like this:

numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];

It would probably be best to not set the currencySymbol at all. Then when the locale is, say, Thailand (@"th_TH"), you get

numberFormatter.internationalCurrencySymbol   "THB"
numberFormatter.currencySymbol                "฿"
numberFormatter.currencyCode                  "THB"