0

I keep getting an UnparseableNumberException while parsing the FR currency.

My code:

Locale locale = new Locale("fr", "FR");
NumberFormat numberFormat = NumberFormat
                .getCurrencyInstance(locale);
try {
    Number number = numberFormat.parse("€ 314,00");
    System.out.println("sadsadsadsad"+number.doubleValue());
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Console output:

java.text.ParseException: Unparseable number: "€ 314,00"
    at java.text.NumberFormat.parse(Unknown Source)
    at PS6.main(PS6.java:26)
Magisch
  • 7,312
  • 9
  • 36
  • 52
user2142786
  • 1,484
  • 9
  • 41
  • 74
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Nov 14 '16 at 08:45

1 Answers1

3

In France, which is using euro as currency, prices are formatted with the currency symbol at the end, with a space before it; not at the beginning. As such, "€ 314,00" is invalid input because the "€" symbol is misplaced, and you should have "314,00 €" instead.

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
Number number = numberFormat.parse("314,00 €");
System.out.println(number);

Also, instead of creating a new locale, you can use the built-in Locale.FRANCE.

Note that this isn't true for all countries using the euro; for example, Ireland places the euro sign before the number, without space (and with a dot instead of a comma as decimal separator). So for the locale new Locale("en", "IE"), the correct format would be "€314.00"...

Tunaki
  • 132,869
  • 46
  • 340
  • 423