映射有時是一對多...喜歡歐元在許多國家(語言環境)使用...
僅僅因爲貨幣代碼是相同的如本例所示的格式可能會有所不同:
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