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)?
There's no absolute rules here but a couple of guiding principles:
- 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);
- Remember that different currencies can use the same symbol (eg $ is used by Australian and US Dollars) and that there are many currency symbols;
- 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
- 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.
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));
}
}