0

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.
DAK
  • 282
  • 1
  • 18

3 Answers3

2

unless you really need to do that manually, i would rather use a java money

<dependency>
  <groupId>org.javamoney</groupId>
  <artifactId>moneta</artifactId>
  <version>1.4.1</version>
  <type>pom</type>
</dependency>

Never used it but it sounds it could solve your problem, for more info take a look into docs https://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide.adoc

pezetem
  • 2,503
  • 2
  • 20
  • 38
1

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 DecimalFormat using the constructor with two params, the first is the pattern, the second are the symbols to be used:

Creates a DecimalFormat using the given pattern and symbols. Use this constructor when you need to completely customize the behavior of the format.

You can find details on the official guide here

Here is a working example:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
symbols.setCurrency(Currency.getInstance("EUR"));
DecimalFormat df = new DecimalFormat("¤###,###.00", symbols);
System.out.println(df.format(5000.4));  // Will print €5.000,40    

Here is a description of the pattern ¤###,###.00 that is used on the previous example

¤       is the currency symbol (in our case will be replaced by the EUR symbol)
###,### is the integer part of the number. Digits are grouped in group of 3 
.00     is the decimal part of the number. If less than 2 decimal numbers are presented zeroes are 
        added to the string so to have exactly 2 decimal numbers 

This example doesn't take care about the locale because all the symbols used in the formatted number are replaced explicitly.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

It seems you are asking for an international standard to display amounts in euros.

For official, formal, legal text the ISO standard EUR (like USD for dollar) is required. For commercial and other human areas (like $) is used.

But then every language seems to maintain its old currency's format: British EUR before the amount, many other European languages after the amount. The same holds for decimal and thousands separator, which vary per country.

There is a non-breaking space between euro symbol and amount. So using the widely used SI standard regarding decimal separator (comma!) one would get:

9.999,99\u00A0€

Try see whether Locale.FRANCE or GERMANY suffices.

Note: this (placement of currency to the right) allows amounts with currency placed in a single right-aligned column.

In order to prevent law suits in the US that the above item just costs 10 dollars, one might use a non-breaking space, or a non-breaking half space:

9.999.999,99\u00A0€
9\u202F999\u202F999,99\u00A0€

looking more or less like (and not word wrapped at line end):

9.999.999,99 €
9 999 999,99 €

The Swiss (typical) solution is to use the apostrophe ' as thousands' separator.

The comma is IMHO US-legally unproblematic as SI, Système International (d'unités)), Internation System of Units, is a representative standard.

Euro cents have no cent symbol, but are shown in euros.

  • U+20AC = € (euro symbol)
  • U+A0 = non-breaking white space
  • U+202F = non-breaking half space

So, I do not know of a pan-european standard. But I am no financial expert.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138