Reputation: 5507
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.
Upvotes: 7
Views: 10643
Reputation: 81
Great explanation and code is here: https://drivy.engineering/multi-currency-java/
I would say use the standard, instead of pleasing your designers.
Upvotes: 1
Reputation: 1255
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...
Upvotes: 10
Reputation: 3901
The NumberFormat.getCurrencyInstance() can be used to format a number according to the currency of a particular locale in the backend. And the formatted currency can be passed to FE for rendering. For example:
double num = 976_543.21;
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(new Locale("fr","FR"));
System.out.println(defaultFormat.format(num)); //output 976 543,21 €
Locale us = new Locale("en", "US");
NumberFormat usFormat = NumberFormat.getCurrencyInstance(us);
System.out.println(usFormat.format(num)); //output $976,543.21
Upvotes: 0
Reputation: 7730
You can use Globalizejs for your requirement
A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
It's pretty easy to start with and you need not worry about placing the currency code in front or at the end, The library will take care according to locale and currencyCode.
Example:
var formatter;
Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD" );
formatter( 9.99 );
// > "$9.99"
in different locale:
var deFormatter = Globalize( "de" ).currencyFormatter( "EUR" );
deFormatter( 9.99 );
// > "9,99 €"
Upvotes: 0