4

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];
}
James Campbell
  • 3,511
  • 4
  • 33
  • 50
  • `NSNumberFormatterCurrencyStyle` Currency value can be negative :O??? – Lithu T.V Aug 05 '13 at 12:12
  • Please search for existing answers before you ask a new one. – John Parker Aug 05 '13 at 12:14
  • BTW, the `[currencyStyle setNumberStyle:@"NSNumberFormatterCurrencyStyle"];` line is incorrect; use `[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];` as you did later. – Rob Aug 05 '13 at 12:28

1 Answers1

2

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"];
zahreelay
  • 1,742
  • 12
  • 18
  • This is because you need to take locale into account with numbers, dates, etc. unfortunately what cocoa does precisely per locale is not well documented. Some is from ICU and some is baked in by Apple. I suggest filling a documentation bug with Apple. – uchuugaka Aug 05 '13 at 13:16
  • well... it the same wihn pl_PL on iOS8 – Marcin Sep 06 '14 at 13:31