Question

I want the formatter to show -£4.00 as -£4.00 but it keeps displaying it as (£4.00), why is this ?

This Function below turns strings "00000014" into "£00.14" it should turn negative values too. But the number formatter seems to add brackets.

Code is 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];
}
Was it helpful?

Solution

This 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 correct.

I can see two ways :

  1. create your own formatter :

    [currencyStyle setFormat:@"¤#,##0.00"];

2.Another not so proper way is use australia's identifier. It formats the same way u would need .

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top