Search code examples
foundationnsnumberformatter

NSNumberFormatter currency negative number


My current code is

NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(@"CURRENCY", @"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];

I'm using NSLog to check the string currency and it's printing "($ 70.00)". I changed "(", ")" symbol. How can I achieve this:

( $ 70.00 ) -> - $70.00 or $ -70.00

Solution

  • You have to set the negative format. Add this line:

    [f setNegativeFormat:@"-¤#,##0.00"];
    

    but maybe you just want to set the locale with [f setLocale:] instead of setting every format option on your own.

    (and next time post code that does compile.)