Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumberFormatter currency symbol

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?

like image 234
kas-kad Avatar asked Jul 14 '14 18:07

kas-kad


1 Answers

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"
like image 86
Mundi Avatar answered Sep 28 '22 09:09

Mundi