NSNumberFormatter displays parentheses by negative values

I want the formatter to show - $ 4.00 as - $ 4.00, but it continues to show it as ($ 4.00), why is this?

This function below turns the strings "00000014" into "ยฃ 00.14", it should also show negative values. But the formatting number seems to add brackets.

Code below:

+(NSString *)pfsCurrencyAmountString:(NSString *)amount CurrencyCodeAsString:(NSString *)code{ if (amount == nil) { return nil; } int amountLength = [amount length]; NSMutableString *amountMutable = [NSMutableString stringWithString:amount]; if (amountLength > 2) { [amountMutable insertString:@"." atIndex:amountLength - 2]; } NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; [currencyStyle setNumberStyle:@"NSNumberFormatterCurrencyStyle"]; [currencyStyle setLocale:locale]; [currencyStyle setCurrencyCode:code]; NSNumber * balance = [currencyStyle numberFromString:amountMutable]; [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; return [currencyStyle stringFromNumber:balance]; } 
+4
source share
1 answer

That seems weird. When you use

 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 

It prints in brackets, but if you use other identifiers for other countries, it prints correctly.

I see two ways:

  • create your own formatter:

    [currencyStyle setFormat: @ "ยค #, ## 0.00"];

2. Another incorrect way is to use the Australia ID. It formats the same that you need.

 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"]; 
+2
source

Source: https://habr.com/ru/post/1495309/


All Articles