13

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?

kas-kad
  • 3,736
  • 1
  • 26
  • 45

1 Answers1

11

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"
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • what is your current locale? – kas-kad Jul 14 '14 at 19:20
  • Wrong. You can set the locale for the formatter as explained above. – Mundi Jul 14 '14 at 19:26
  • Ofcourse I can. But how do you find correct locale if the user selected AED currency? or something more exotic. I don't want to hardcode the currency-locale map. – kas-kad Jul 14 '14 at 19:28
  • Then just don't set the `currencyCode`. Let the locale take care of it. In my example, I force a US system to use the € symbol. – Mundi Jul 14 '14 at 19:34
  • @purrrminator: Mundi is right, the correct way to do it is to set the locale of the country/region from which the currency is and to not touch the `currencyCode` and `currencySymbol`. See also the sample code from [`-[SKProduct price]`](https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/Reference/Reference.html#//apple_ref/occ/instp/SKProduct/price). – DarkDust Jul 15 '14 at 17:53
  • @DarkDust thanks for the sample code, it's good to see the solution provided by Apple – kas-kad Jul 16 '14 at 14:09
  • @DarkDust am I getting it right that there is still no way to get correct locale for random currency? The only way is to hardcode a correct locale id for each supported currency? – kas-kad Jul 16 '14 at 14:14
  • @purrrminator I don't know whether it's the only way (I doubt it), but it's the way that seems to work the best. – DarkDust Jul 16 '14 at 15:18
  • Apple has removed the sample code from the documentation. Here is the web archive link: https://web.archive.org/web/20140604142312/https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/Reference/Reference.html – Luke Dec 19 '20 at 22:33