【问题标题】:Proper currency format when not displaying the native currency of a culture不显示文化的本国货币时的正确货币格式
【发布时间】:2016-09-09 17:41:59
【问题描述】:

如果您正在格式化不是当前文化的本地货币的货币,那么格式化货币的正确方法是什么?

例如,如果我为 fr-FR 文化格式化美元,我是像 en-US 文化 ($1,000.00) 一样格式化还是作为 fr-FR 文化但更改欧元符号美元符号 (1 000,00 $)。也许是其他东西($1 000,001 000,00 USD)?

【问题讨论】:

  • 这不应该属于其他一些 SE 网站吗?...也许是 Economics SE? economics.stackexchange.com这个问题和StackOverflow的范围有什么关系?

标签: formatting currency


【解决方案1】:

这里没有绝对的规则,但有几个指导原则:

  1. 尝试使用该区域设置的数字格式(例如,美国的 1,000.00 在德国将显示为 1'000,00);
  2. 请记住,不同的货币可以使用相同的符号(例如,澳元和美元使用 $)并且有many currency symbols
  3. 如果您的网站是“单一”货币,那么只需使用该货币的正确符号即可。我指的是亚马逊、旅游网站、购物网站等网站。这些网站是单一货币的,因为它们一次只使用一种货币。例如,他们不会同时展示马来西亚林吉特和新加坡元;和
  4. 如果您的网站是多币种的,则根本不要使用该符号:使用ISO 4217 currency names and code elements 定义的国际标准三字母货币代码。 xe.com 等网站就属于这一类。

【讨论】:

【解决方案2】:

如果你总是想显示符号,这里有一个实用程序类:

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));
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2011-06-18
    • 2018-08-20
    • 2012-01-29
    相关资源
    最近更新 更多