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:
I am using java.util.Currency.
Tip: To quickly apply the Currency format, select the cell or range of cells that you want to format, and then press Ctrl+Shift+$. Like the Currency format, the Accounting format is used for monetary values. But, this format aligns the currency symbols and decimal points of numbers in a column.
Select the cells that you want to format and then, in the Number group on the Home tab, click the down arrow in the Number Format box. Choose either Currency or Accounting.
For US dollars, the symbol '$' is sufficient abbreviation, unless there is a mixture of dollar currencies in the text. For other dollar currencies, '$' should be prefixed with the country abbreviation. For all other currencies, write the figure first followed by the currency name, for example, '100 million yuan'.
It is composed of the country code ( US ), followed by the letter “D” for “dollar.” Write the dollar figure first, followed by a non-breaking space and the code: 350 000 USD.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With