Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NSNumberFormatter's negative format from (xxx.xx) to -xxx.xx

I want to change my NSNumberformatter from displaying negative numbers with parenthesis around them to putting the minus sign in front (or whatever the localized standard is).

I would assume I could do this with setNegativeFormat:

but reading Apple's oh so thorough docs I am left scratching my head:


setNegativeFormat:

Sets the format the receiver uses to display negative values.

- (void)setNegativeFormat:(NSString *)aFormat

Parameters aFormat A string that specifies the format for negative values.

Availability Available in iPhone OS 2.0 and later.

See Also – negativeFormat

Declared In NSNumberFormatter.h


what are my options for aFormat?!? C'mon Doc Writers, would a link here kill you?

edit: for what it's worth here's the declaration:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

It's important for me to retain the localized currency symbol & decimal places whatever they may be. So [currencyFormatter setNegativeFormat:@"-#,##0.00"] probably won't work as currency is missing and 2 decimals can't be assumed for all currencies.

like image 790
Meltemi Avatar asked Jun 18 '09 07:06

Meltemi


1 Answers

If you take a look at the "Format Strings" section in the Data Formatting Programming Guide For Cocoa:

The format string uses the format patterns from the Unicode Technical Standard #35 (this reference is to version tr35-6; formatters for Mac OS X v10.4 use version tr35-4).

Edit:

If you want to set a format string based on currencies, you can use the ¤ character, for example:

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

This will add the currency symbol for the current localization in place of the ¤ character.

Therefore, applying the same concept to the negative format string:

[formatter setFormat:@"-¤#,##0.00"];

This will also apply the currency symbol in place of the ¤ for the current localization.

like image 166
Alex Rozanski Avatar answered Oct 21 '22 05:10

Alex Rozanski