如何从货币代码中获取NumberFormat实例?

如何才能获得与ISO 4217货币代码(例如“EUR”或“USD”)对应的NumberFormat (或DecimalFormat )实例,以便正确格式化价格?

注1:我遇到的问题是NumberFormat / DecimalFormat类有一个getCurrencyInstance(Locale locale)方法,但我无法弄清楚如何从ISO 4217货币代码获取Locale对象。

注意2:还有一个java.util.Currency类,它有一个getInstance(String currencyCode)方法(为给定的ISO 4217货币代码返回Currency实例),但我再也无法弄清楚如何从Currency对象获取到NumberFormat实例…

我不确定我是否正确理解这一点,但您可以尝试以下方法:

 public class CurrencyTest { @Test public void testGetNumberFormatForCurrencyCode() { NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(2); Currency currency = Currency.getInstance("USD"); format.setCurrency(currency); System.out.println(format.format(1234.23434)); } } 

输出:

 1,234.23 

请注意,我分别设置了最大小数位数, NumberFormat.setCurrency没有触及最大小数位数:

设置货币值格式时设置此数字格式使用的货币。 这不会更新数字格式使用的最小或最大小数位数。

映射有时是一对多…就像欧元在许多国家(区域)使用…

仅仅因为货币代码相同,格式可能会有所不同,如下例所示:

 private static Collection getLocalesFromIso4217(String iso4217code) { Collection returnValue = new LinkedList(); for (Locale locale : NumberFormat.getAvailableLocales()) { String code = NumberFormat.getCurrencyInstance(locale). getCurrency().getCurrencyCode(); if (iso4217code.equals(code)) { returnValue.add(locale); } } return returnValue; } public static void main(String[] args) { System.out.println(getLocalesFromIso4217("USD")); System.out.println(getLocalesFromIso4217("EUR")); for (Locale locale : getLocalesFromIso4217("EUR")) { System.out.println(locale + "=>" + NumberFormat.getCurrencyInstance(locale).format(1234)); } } 

产量

 [en_US, es_US, es_EC, es_PR] [pt_PT, el_CY, fi_FI, en_MT, sl_SI, ga_IE, fr_BE, es_ES, de_AT, nl_NL, el_GR, it_IT, en_IE, fr_LU, nl_BE, ca_ES, sr_ME, mt_MT, fr_FR, de_DE, de_LU] pt_PT=>1.234,00 € el_CY=>€1.234,00 fi_FI=>1 234,00 € en_MT=>€1,234.00 sl_SI=>€ 1.234 ga_IE=>€1,234.00 fr_BE=>1.234,00 € es_ES=>1.234,00 € de_AT=>€ 1.234,00 nl_NL=>€ 1.234,00 el_GR=>1.234,00 € it_IT=>€ 1.234,00 en_IE=>€1,234.00 fr_LU=>1 234,00 € nl_BE=>1.234,00 € ca_ES=>€ 1.234,00 sr_ME=>€ 1.234,00 mt_MT=>€1,234.00 fr_FR=>1 234,00 € de_DE=>1.234,00 € de_LU=>1.234,00 € 

区域设置既可用于获取区域设置的标准货币,也可用于在指定的区域设置中正确打印任何货币符号。 这是两个截然不同的操作,并没有真正相关。

在Java国际化教程中 ,您首先使用Locale或ISO代码获取Currency的实例。 然后,您可以使用其他区域设置打印该符号。 因此,如果您从en_US Locale获得US货币,并调用getSymbol(),它将打印“$”。 但是,如果您使用British Locale调用getSymbol(Locale),它将打印“USD”。

因此,如果您不关心当前用户的区域设置,并且您只关心货币,那么您可以在所有情况下忽略区域设置。

如果您关心根据当前用户正确表示货币符号,则需要获取特定于用户所在位置的用户区域设置。

为了完整性,虽然我从未使用它,但您可能想尝试一下Joda Money 。 如果它和Joda-Time一样好,它可能比标准的JDK更简单,更强大。

尝试以下方法:

 private static NumberFormat getNumberFormat(String currencyCode) { Currency currency = Currency.getInstance(currencyCode); Locale[] locales = NumberFormat.getAvailableLocales(); for (Locale locale : locales) { NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); if (numberFormat.getCurrency() == currency) return numberFormat; } return null; } 
 public class PriceHelper { public static String formatPrice(Context context, String currencyCode, double price) { if (price == 0) { return context.getString(R.string.free); } Currency currency = Currency.getInstance(currencyCode); NumberFormat format = NumberFormat.getCurrencyInstance(); format.setCurrency(currency); return format.format(price); } } 

这是一个方法:

 String formatPrice(double amnt) { NumberFormat formatter = NumberFormat.getCurrencyInstance(); Currency currency = Currency.getInstance("USD"); formatter.setCurrency(currency); return formatter.format(amnt); }