I have currency code and value in double. Need to do formating for Currencies.I tried with NumberFormat and Locale but in this case for example EURO has different Locale related to countries. How can I achieve this?, Is there any common format for Euro?
format.setCurrency(Currency.getInstance("EUR"));
format.setMaximumFractionDigits(2);
System.out.println(format.format(dbl));
Locale[] locales = NumberFormat.getAvailableLocales();
for(Locale lo : locales){
NumberFormat format = NumberFormat.getCurrencyInstance(lo);
if(NumberFormat.getCurrencyInstance(lo).getCurrency().getCurrencyCode().equals("EUR")){
System.out.println( NumberFormat.getCurrencyInstance(lo).getCurrency().getCurrencyCode()+"-"+lo.getDisplayCountry() +"-"+NumberFormat.getCurrencyInstance(lo).getCurrency().getSymbol() +format.format(dbl));
}
}```
Sorry previous question was closed.
There is no need to import an external java library that you need to learn and increase the size of your application. It is possible to use the
DecimalFormatusing the constructor with two params, the first is the pattern, the second are the symbols to be used:You can find details on the official guide here
Here is a working example:
Here is a description of the pattern ¤###,###.00 that is used on the previous example
This example doesn't take care about the locale because all the symbols used in the formatted number are replaced explicitly.