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

Adi*_*ain 18 java

为了正确格式化价格,我如何获得与ISO 4217货币代码(例如"EUR"或"USD")对应的NumberFormat(或DecimalFormat)实例?

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

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

esa*_*saj 21

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

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));
    }   
}
Run Code Online (Sandbox Code Playgroud)

输出:

1,234.23
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这个解决方案并不理想,因为Spencer Kormos提到的货币有时会以"CA $ 0.69"而不是"$ 0.69"的形式出现,但发现这个解决方案最简单,最干净,需要按下,所以请继续使用.刚做了一个改变 - 而不是调用`format.setMaximumFractionDigits(2);`,我正在调用`format.setMaximumFractionDigits(currency.getDefaultFractionDigits());`而不是. (7认同)

Ada*_*dam 6

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

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

private static Collection<Locale> getLocalesFromIso4217(String iso4217code) {
    Collection<Locale> returnValue = new LinkedList<Locale>();
    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));
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

[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 €
Run Code Online (Sandbox Code Playgroud)


Spe*_*mos 5

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

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

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

如果你关心代表的基础上正确的当前用户的货币符号,那么你就需要获得区域设置用户的特定于用户的位置.