Detect which side the current currency is on

821 views Asked by At

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]];
1

There are 1 answers

3
rob mayoff On BEST ANSWER

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.