Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java number parsing in French locale

 
Rahul Vhatkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.setDecimalFormatSymbols(symbols);

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
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic