File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java number parsing in French locale Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java number parsing in French locale" Watch "Java number parsing in French locale" New topic

Java number parsing in French locale

Rahul Vhatkar

Joined: Aug 24, 2009
Posts: 9
Java number parsing in French locale using DecimalFormat

Following code is not throwing an error -

Locale lcl = new Locale("fr","FR");
NumberFormat frmtr = NumberFormat.getInstance(lcl);
DecimalFormat df = (DecimalFormat) frmtr;
DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');

df.parse("1 2 3");

I thought the parse statement would throw an error. But it parses the string successfully and gives me a number 123.
How can I make it throw an error for such numbers which are wrong from French locale perspective?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Should that code throw an error? You explicitly set the thousands seperator to a blank space (ignoring the value set for the French locale which should be '.' I think) then parse a number less than 1000 so the 1000s seperator won't be used anyway. Or am I missunderstanding what you are trying to do?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40418
Welcome to JavaRanch

I presume, Paul, the group separator will be accepted providing it appears in a regular location. In some number systems (I think commonly in India) the group separator comes evry 4th digit rather than every 3rd. So I tried it; some of the following code is copied and pasted from Rahul Vhatkar's:So that is obviously not the explanation. I couldn't get much farther by reading the DecimalFormat and DecimalFormatSymbols documentation. Sorry.

Anybody else know?
I agree. Here's the link:
subject: Java number parsing in French locale