• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Java number parsing in French locale

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic