问题
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.
回答1:
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.
回答2:
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...
回答3:
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.##"
回答4:
All of the answers given assume that the currency is to two decimal places and also uses commas as thousand separators. Obviously there are a number of currencies that don't conform to this standard, http://en.wikipedia.org/wiki/Decimal_mark, so I use the following technique
NSString * formattedBalance = [currencyFormatter stringFromNumber:balance];
if([formattedBalance rangeOfString:@"("].location != NSNotFound ) {
formattedBalance = [NSString stringWithFormat:@"-%@",[[formattedBalance stringByReplacingOccurrencesOfString:@")" withString:@"" ]stringByReplacingOccurrencesOfString:@"(" withString:@"" ]];
}
来源:https://stackoverflow.com/questions/1011183/change-nsnumberformatters-negative-format-from-xxx-xx-to-xxx-xx