How to determine where to place the currency symbol when formatting a number?
currency-formatting, internationalization, java, localization
Solution
I found this question while searching for a way to determine whether the currency symbol should be placed at the start or end of a currency for a specific locale. For others who might be looking for something similar, I ended up creating the following class to test this:
public class Test {
private static Locale[] locales = Locale.getAvailableLocales();
static double decimal = 789456123.098;
public static void main(String[] args) {
for(Locale locale : locales){
DecimalFormat decimalFormatter = (DecimalFormat)NumberFormat.getInstance(locale);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
DecimalFormatSymbols symbols = decimalFormatter.getDecimalFormatSymbols();
String currencySymbol = symbols.getCurrencySymbol();
String formattedCurrency = currencyFormatter.format(decimal);
System.out.print(formattedCurrency + ";" + locale.getDisplayCountry() + ";" + locale.getDisplayLanguage() );
System.out.println(";" + currencySymbol + ";" + currencySymbolAtStart(currencySymbol, formattedCurrency, true));
}
}
private static boolean currencySymbolAtStart(String currencySymbol, String formattedCurrency, boolean defaultAtStart){
if(formattedCurrency.startsWith(currencySymbol)){
return true;
}else if(formattedCurrency.endsWith(currencySymbol)){
return false;
}else{
return defaultAtStart;
}
}
}
Pasting the results into a spreadsheet looks something like this:
There is one caveat here to be aware of. For languages that are read right to left, the currency symbol might appear on the right in the formatted text, yet technically, since you are reading from right to left, the currency still "starts with" the currency symbol, hence the result will return "true". See Egypt-Arabic in the screen snip above...
Problem
I am not sure if the same question is asked by any other. I have not seen this question in stackoverflow so far, hence posting the same. I have a requirement where I need to format the numbers based on the currency. Let me explain the above table to give you more context of my question. For UnitedStates, numbers are formatted as USD976,543.21, here the currency symbol is USD which is placed in the front before numbers. This is little different for fr-FR locale, here € symbol is placed after the digits. All my formatting is happening in the front end and not the backend. I am giving a format in the form of # to the FE, then our home grown code formats the digits in that format. Example, for United states, I am supplying USD###,###,###.####. Here, I need to let the front end know the below information so that front end formats the same in a prompt manner. My question is: Is there any way to get the below information in the backend: - Where to place the currency, start of the number or towards ending? - Is there any space between number and symbol? Look at USD976,543.21 and 976 543,21 € . There is space between currency symbol and number for EURO but not for USD. I am using java.util.Currency.