• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parse decimal value  RSS feed

 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I want to parse a user entered decimal string into a decimal number. I also want to take into account the different locale. For example in my country, 43,5 is a valid decimal number.

I have tried the following code:

NumberFormat nf = NumberFormat.getNumberInstance(getMyLocale());
DecimalFormat df = (DecimalFormat)nf;
Float num = Float.valueOf(df.parse(ftf.getText()).toString());

It does not work, becuase 45_76 does not throw ParseException, instead df.parse return just the first two digits.
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
up
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gberes:
It does not work, becuase 45_76 does not throw ParseException, instead df.parse return just the first two digits.


Which is eactly what it is supposed to do. From the javadoc
Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
A ParseException will only be thrown if a number cannot be parsed from the beginning of the String.
[ November 20, 2008: Message edited by: Joanne Neal ]
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne!

I want to accept only those decimal numbers which are parseable based on the current locale, an throw an error if they are not.

How can i achieve that?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only want to work with the current locale then Float.valueOf(String) should work for you.
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not the case, because Float.valueOf does not work on 47,6, and that format it is perfectly valid in my country.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I misread the javadoc.

You could try the overloadded version of DecimalFormat.parse that takes a position to start the parsing at and resets it to the position it stops parsing at. If you initialise the position to zero and then compare the final position to the length of your original string, it should tell you whether it's parsed the whole string or not.

Note - I've never used this method. the above is just based on my reading of the Javadoc (which as you can see is not infallible )
 
J. Noah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try to parse with the standard methods, but put a try-catch clause around it. If you can't parse using a standard method, then try your custom parse case.
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its funny how java does totaly unexpected things. If i enter 47,543, then s is 47543. It just drops the ','. Now i really don't know how to solve this simple problem.


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gabriel Beres:
Its funny how java does totaly unexpected things. If i enter 47,543, then s is 47543. It just drops the ','. Now i really don't know how to solve this simple problem.




df.parseObject returns an instance of Number. Now, as your string contains a decimal, it will return either a Float or a Double. If you look at the Javadoc for the toString() method for those two classes, it describes the format of the string it will return. Neither includes separation of the thousands by any character.

If you want to retain the comma, then why not just use your original string once it has passed validation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!