Proper currency format when not displaying the native currency of a culture
Asked Answered
A

2

16

What is the proper way to format currency if you are formatting a currency that is not the native currency of the current culture?

For example, if I am formatting US Dollars for a fr-FR culture do I format it like a en-US culture ($1,000.00) or as an fr-FR culture but changing the Euro symbol to a US Dollar symbol (1 000,00 $). Perhaps something else ($1 000,00 or 1 000,00 USD)?

Amboceptor answered 12/5, 2009 at 0:41 Comment(1)
Shouldn't this belong on some other SE website?... maybe Economics SE? economics.stackexchange.com How is this question related to the scope of StackOverflow?Ideally
T
31

There's no absolute rules here but a couple of guiding principles:

  1. Try and use the number format for that locale (eg 1,000.00 in the US would be displayed as 1'000,00 in Germany);
  2. Remember that different currencies can use the same symbol (eg $ is used by Australian and US Dollars) and that there are many currency symbols;
  3. If your site is "single" currency then just use the correct symbol for that currency. By this I mean sites like Amazon, travel sites, shopping sites and so on. These sites are single currency in the sense they are one currency at a time. They won't be displaying Malaysian Ringits and Singapore Dollars at the same time, for example; and
  4. If your site is multi-currency then don't use the symbol at all: use the international standard three letter currency code as defined by ISO 4217 currency names and code elements. Sites like xe.com fit into the category.
Thou answered 12/5, 2009 at 0:53 Comment(2)
Deleted my original answer as this is better +1Roesch
Very clear advice - For points #1 and #2 check here for how to do this in c# https://mcmap.net/q/550698/-currency-formatting/1071302Atwell
P
0

If you always want to display the symbol, here is an utility class:

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


}
Peart answered 8/6, 2010 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.