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.