3

How can I check if which side the currency symbol is at? For example, in the United States, the symbol would be like this: "$56.58", but in France it would be like this: "56.58€". So how would I be able to detect if it's on the right or left side?

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setLocale:[NSLocale currentLocale]];
Eric
  • 3,811
  • 5
  • 15
  • 18
  • Try to find out the ASCII value of the very first and last character of the price string. If first one of them has ASCII value not in range (48-57) then currency symbol is at left side, otherwise at right side. – Sauvik Dolui Jun 11 '15 at 02:08
  • Can you show me a sample please? – Eric Jun 11 '15 at 02:09
  • Curious - why do you need to know this? – rmaddy Jun 11 '15 at 02:24
  • The reason I want this, is because I have a UITextField, and a UILabel. The label is gonna take the symbol and the textField will be an amount of money. Therefore I need to know which side the label should be on. You're probably wondering why I just skip the label and format the textField? The answer is, because it's just impossible for me to implement. I tried it for days without success. That's why I'm gonna do this "hack". – Eric Jun 11 '15 at 02:26
  • @rmaddy If you have a better solution, I'm all ears!! – Eric Jun 11 '15 at 02:31
  • 2
    No need for the label. Just use the text field with a currency formatter. If you can't make it work, post a question about it with details. – rmaddy Jun 11 '15 at 02:35
  • @rmaddy I asked the question here: http://stackoverflow.com/questions/30742666/format-currency-with-uitextfield – Eric Jun 11 '15 at 03:22

1 Answers1

4

If you just want to format a number as currency, set the formatter's numberStyle to NSNumberFormatterCurrencyStyle and then use its stringFromNumber: method.

If, for some reason, you really want to know the position of the currency symbol in the format for the formatter's locale, you can ask the formatter for its positiveFormat and look for the character ¤ (U+00A4 CURRENCY SIGN).

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterCurrencyStyle;

f.locale = [NSLocale localeWithLocaleIdentifier:@"en-US"];
NSLog(@"%@ format=[%@] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
    (unsigned long)[f.positiveFormat rangeOfString:@"\u00a4"].location);

f.locale = [NSLocale localeWithLocaleIdentifier:@"fr-FR"];
NSLog(@"%@ format=[%@] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
    (unsigned long)[f.positiveFormat rangeOfString:@"\u00a4"].location);

f.locale = [NSLocale localeWithLocaleIdentifier:@"fa-IR"];
NSLog(@"%@ format=[%@] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
    (unsigned long)[f.positiveFormat rangeOfString:@"\u00a4"].location);

Result:

2015-06-10 21:27:09.807 commandline[88239:3716428] en-US format=[¤#,##0.00] ¤-index=0
2015-06-10 21:27:09.808 commandline[88239:3716428] fr-FR format=[#,##0.00 ¤] ¤-index=9
2015-06-10 21:27:09.808 commandline[88239:3716428] fa-IR format=[‎¤#,##0] ¤-index=1

Note that in the fa-IR case, the symbol is not the first or last character in the format string. The first character (at index zero) is invisible. It's U+200E LEFT-TO-RIGHT MARK.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • So would I be able to do the following: `if ([f.positiveFormat rangeOfString:@"\u00a4"].location == 0)` – Eric Jun 11 '15 at 02:23
  • The reason I want this, is because I have a UITextField, and a UILabel. The label is gonna take the symbol and the textField will be an amount of money. Therefore I need to know which side the label should be on. You're probably wondering why I just skip the label and format the textField? The answer is, because it's just impossible for me to implement. I tried it for days without success. That's why I'm gonna do this "hack". – Eric Jun 11 '15 at 02:26
  • Take a look at my update. I suppose you could do something like `if ([f.positiveFormat rangeOfString:@"\u00a4"].location < f.positiveFormat.length / 2) ...`. – rob mayoff Jun 11 '15 at 02:29