将NSNumberFormatter的负格式从(xxx.xx)更改为-xxx.xx [英] Change NSNumberFormatter's negative format from (xxx.xx) to -xxx.xx
问题描述
我想更改我的NSNumberformatter从显示负数的圆括号周围放置减号在前面(或任何本地化的标准是)。
我假设我可以用setNegativeFormat:
,但是读苹果的哦,抓住我的头:
setNegativeFormat :
设置接收器用于显示负值的格式。
- (void)setNegativeFormat:(NSString *)aFormat
参数
aFormat
负值的格式。
可用性
适用于iPhone OS 2.0及更高版本。
另请参见
NSNumberFormatter .h
aFormat的选项是什么?
编辑:这里的价值是什么:
NSNumberFormatter * currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
对我来说保留本地化的货币符号&小数位数,无论它们是什么。所以[currencyFormatter setNegativeFormat:@ - #,## 0.00]可能不工作,因为货币缺失,不能假设所有货币的2位小数。
如果您查看数据格式编程指南对于Cocoa :
格式字符串使用 Unicode技术标准#35 (此引用是版本tr35-6; Mac OS X v10.4的格式化程序使用版本 tr35-4 )。
编辑:
如果您要根据货币设置格式字符串,可以使用 ¤
字符,例如:
[formatter setFormat:@¤#,## 0.00 ];
这将添加当前本地化的货币符号代替
<$>
<$>
[formatter setFormat:@ - ¤#,## 0.00];
这也将使用货币符号代替¤
用于当前本地化。
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.
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.
这篇关于将NSNumberFormatter的负格式从(xxx.xx)更改为-xxx.xx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!