如果您格式化的货币不是当前文化的本机货币,那么格式化货币的正确方法是什么?
例如,如果我为fr-FR文化格式化美元,我会将其格式化为en-US文化( $ 1,000.00 )或作为fr-FR文化,但更改欧元符号美元符号( 1 000,00 $ )。也许别的东西( $ 1 000,00 或 1 000,00 USD )?
答案 0 :(得分:29)
这里没有绝对的规则,只有几条指导原则:
答案 1 :(得分:0)
如果您始终想要显示符号,则此处是实用程序类:
public class Utils {
public static SortedMap<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
@Override
public int compare(Currency c1, Currency c2) {
return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
}
});
for (Locale locale : Locale.getAvailableLocales()) {
try {
Currency currency = Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
}
catch (Exception e) {
}
}
}
public static String getAmountAsFormattedString(Double amount, Double decimals, String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
double doubleBalance = 0.00;
if (amount != null) {
doubleBalance = ((Double) amount) / (Math.pow(10.0, decimals));
}
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(currencyLocaleMap.get(currency));
return numberFormat.format(doubleBalance);
}
public static String getCurrencySymbol(String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
return currency.getSymbol(currencyLocaleMap.get(currency));
}
}