Search code examples
ioscurrencynsnumberformatter

iOS Currency Format - remove the text before the symbol


Is there a way to remove the text that comes before the currency symbol from a string output using NSNumberFormatter?

NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = @"HKD";
NSLog(@"%@", [formatter stringFromNumber:@1000]);

Output: HK$1,000.00

I'd like to know if there is a way to remove the HK before the $ without parsing the string and removing the text. Perhaps a property to set on NSNumberFormatter?


Solution

  • One way to avoid the extra characters is to set the formatter's local to match the chosen currency code. If you want to show HKD values as just $ to a user not in Hong Kong, you need to also set the formatter's locale to a Hong Kong locale.

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatter.currencyCode = @"HKD";
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_HK"];
    NSLog(@"%@", [formatter stringFromNumber:@1000]);
    

    This should show just $1,000.00.