NSNumberFormatter 货币符号错误
我在获取 NSNumberFormatter 的currencySymbol 时遇到问题。
我使用带有货币代码“EUR”的 NSNumberFormatter。
当我格式化价格时,符号是正确的,我得到了 € 符号。
但是,当我只想使用 [formattercurrencySymbol] 方法获取currencySymbol 时,会返回符号 $。
如果我手动设置currencySymbol(例如使用“A”),一切都会正常工作,并且方法[格式化程序currencySymbol]将返回“A”符号。
这是我的代码
// Create formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:@"EUR"];
[formatter setLocale:[NSLocale currentLocale]];
// Log the currency symbol
NSLog(@"[formatter currencyCode] : %@", [formatter currencyCode]);
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);
[formatter setCurrencySymbol:@"A"];
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);
这是控制台结果:
2012-01-17 12:29:11.108[4545:207] [formatter currencySymbol] : $
2012-01-17 12:29:11.109[4545:207] [formatter currencySymbol] : €0.00
2012-01-17 12:29:11.110[4545:207] [formatter currencySymbol] : A
2012-01-17 12:29:11.111[4545:207] [formatter currencySymbol] : A0.00
我无法强制货币符号,因为它可以更改。
有没有办法获得与给定的currencyCode对应的正确的currencySymbol?
谢谢
I have a problem getting the currencySymbol of my NSNumberFormatter.
I use a NSNumberFormatter with a currency code "EUR".
When I format prices, the symbol is correct, I get the € symbol.
However, when I want to get just the currencySymbol with the method [formatter currencySymbol], the symbol $ is returned.
If I manually set the currencySymbol (with "A" for instance) everything will work fine and the method [formatter currencySymbol] will return the "A" symbol.
Here is my code
// Create formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:@"EUR"];
[formatter setLocale:[NSLocale currentLocale]];
// Log the currency symbol
NSLog(@"[formatter currencyCode] : %@", [formatter currencyCode]);
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);
[formatter setCurrencySymbol:@"A"];
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);
Here are the console results :
2012-01-17 12:29:11.108[4545:207] [formatter currencySymbol] : $
2012-01-17 12:29:11.109[4545:207] [formatter currencySymbol] : €0.00
2012-01-17 12:29:11.110[4545:207] [formatter currencySymbol] : A
2012-01-17 12:29:11.111[4545:207] [formatter currencySymbol] : A0.00
I cannot force the currencySymbol since it can change.
Is there a way to get the right currencySymbol corresponding to a given currencyCode ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要获取货币符号,您可以将
NSLocaleCurrencySymbol
与NSLocale
结合使用:要获取本地化名称(“美元” ,“日元”,..)您可以使用
NSLocaleCurrencyCode
:其中
currencyCode
是货币代码(JPY、USD、EUR、 ..) 你感兴趣。To get the currency symbol you can use
NSLocaleCurrencySymbol
withNSLocale
:To get the localized name ('US Dollar', 'Japanese Yen', ..) you can use
NSLocaleCurrencyCode
:where
currencyCode
is the currency code (JPY, USD, EUR, ..) you're interested in.问题可能是
[formatter setLocale:[NSLocale currentLocale]];
覆盖了您之前所做的一些配置。您可能想尝试将该行移动到顶部(就在 alloc/init 行之后)。The issue might be that
[formatter setLocale:[NSLocale currentLocale]];
overwrites some of the configuration you did prior to it. You may want to try movig that line to the top (just after the alloc/init line).试试这个 formatter.currencyCode = @"USD";
在此处提供所需的货币代码,它将返回正确的符号
Try this formatter.currencyCode = @"USD";
Provide the desired Currency Code here it will return proper symbol
我使用了以下代码/方法从浮点值获取货币
i have used the following code/methods to get the currency from a float value