映射有时是一对多...喜欢欧元在许多国家(语言环境)使用...
仅仅因为货币代码是相同的如本例所示的格式可能会有所不同:
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));
}
}
输出
[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 €
该解决方案是不理想的,因为货币有时显示格式为“HK $ 0.69”,而不是由斯宾塞Kormos提到“$ 0.69”,却发现这解决方案是最简单和最干净的,并且需要按压,所以就随之而去。只是做了一个改变 - 而不是调用'format.setMaximumFractionDigits(2);',我正在调用'format.setMaximumFractionDigits(currency.getDefaultFractionDigits());'来代替。 – 2012-03-20 14:59:43
我建议使用的'NumberFormat.getCurrencyInstance()',而不是仅仅'的getInstance()'。 – 2013-08-21 20:42:30
我想这是用于'进口java.text.NumberFormat中;'和'没有进口android.icu.text.NumberFormat;'。看起来android.icu.text.NumberFormat中的NumberFormat中的setMaximumFractionDigits和setCurrency只能在最低API级别24下工作。 – 2017-06-10 19:40:30