NSNumberFormatter 货币负数
我当前的代码是
NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(@"CURRENCY", @"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];
使用 NSLog
来检查字符串货币并打印“($ 70.00)”。 我改变了“(”,“)”符号。我怎样才能做到这一点:
( $ 70.00 ) -> - $70.00 or $ -70.00
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须设置负数格式。
添加此行:
但也许您只想使用
[f setLocale:]
设置区域设置,而不是自己设置每个格式选项。(下次发布可以编译的代码。)
You have to set the negative format.
Add this line:
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.)
我有一个 NSNumber 类别可以执行以下操作:
I have a NSNumber category that does this:
将 negativeFormat 设置为“-”几乎对我有用,但它会丢失货币符号。至少对于 en_US 语言环境,这符合我的需求:
我不确定这是否适合所有语言环境,但它适用于 en_US。
Setting negativeFormat to "-" almost worked for me, but it was dropping off the currency symbol. At least for en_US locale, this fits my needs:
I'm not sure whether this is appropriate for all locales, but it works for en_US.