是否有可能从货币代码中获得货币符号? [英] Is it possible to derive currency symbol from currency code?

查看:153
本文介绍了是否有可能从货币代码中获得货币符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程序使用setCurrencyCode将NSDecimalNumber格式化为货币,但另一个屏幕仅显示货币符号。而不是存储货币代码和符号,是否可以从代码中派生符号?我认为以下可能有效,但它只返回符号$:

My iPhone app formats an NSDecimalNumber as a currency using setCurrencyCode, however another screen displays just the currency symbol. Rather than storing both the currency code and symbol, is it possible to derive the symbol from the code? I thought the following might work, but it just returns the symbol as $:

currencyCode = [dictPrices valueForKey:@"currencyCode"];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setCurrencyCode:currencyCode];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSString *currencySymbol = [numberFormatter currencySymbol];


推荐答案

您想要设置区域设置,而不是货币码。然后,您将从格式化程序属性中获得预期结果(或调整它们)。您还可以从区域设置对象本身获取货币符号等内容。因此,例如,如果您在JPY中显示为日本格式化的数字:

You want to be setting the locale, not the currency code. Then you will get the expected results from the formatters properties (or tweak them). You can also get things like the currency symbol from the locale object itself. So, for example, if you were displaying a number formatted for Japan in JPY:

NSLocale* japanese_japan = [[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"] autorelease];
NSNumberFormatter *fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmtr setLocale:japanese_japan];

现在您的数字格式化工具应该根据货币符号和格式为您提供正确的货币符号和格式符号指定的区域设置。

Now your number formatter should give you the correct symbols for currency symbol and format according to the rules in the specified locale.

这篇关于是否有可能从货币代码中获得货币符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆