Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect which side the current currency is on

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]];
like image 573
Eric Avatar asked Sep 05 '25 16:09

Eric


1 Answers

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.

like image 188
rob mayoff Avatar answered Sep 08 '25 10:09

rob mayoff