views:

753

answers:

3

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.

A: 

In this case it is looking for a format NSString. Look here for format string details.

If you want the negative of 12,345.67 to display as -12,345.67, then I believe the correct NSString value is @"-#,##0.00"

I also noted the following sentence in the document linked above:

If you don’t specify a format for negative values, the format specified for positive values is used, preceded by a minus sign (-).

EDIT:
Update for 10.4 and after: Here is a PDF describing behavior in 10.4 and after
And, as linked from that document, here is the data on the required format for 10.4 and after.
From this document, it appears the correct string may be: @"-#,##0.##"

Demi
I think that link is for the old style NSNumberFormatter (10.0 - 10.3). I'm using the 10.4+ version (Cocoa-Touch). I thought that "-#,##0.00" style was abandoned due to localization issues?!?
Meltemi
"If you don’t specify a format for negative values, the format specified for positive values is used, preceded by a minus sign (-)." Might be true for 10.0-10.3 but i'm getting parenthesis "(...)"
Meltemi
edited response with 10.4 info.
Demi
+2  A: 

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.

Perspx
so when I add: "[currencyFormatter setNegativeFormat:@"-#,##0.00"];" after I set the formatter to NSNumberFormatterCurrencyStyle I lose the currency symbol. I could force in a "$" but that defeats the purpose of the CurrencyStyle in other Locales. Isn't there some way to just say something like setNegativeFormat:MinusSignFormat or something?!?
Meltemi
Take a look at my edit.
Perspx
Before you could edit I was able to find that "¤" character thanks to your link. So I'll give you the gold star! Thanks!
Meltemi
A: 

OK, so the answer that I got working is:

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

the key is this whatzit? character "¤". No idea what it's called? anyone? but it represents the localized currency in these format strings...

Meltemi
I'm not sure that it has a name, but it's Unicode symbol 00A4
Perspx
Its names, according to UnicodeChecker, are “currency sign” and “other currency symbol”.
Peter Hosey