Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that a string is parseable to a double? [duplicate]

Is there a native way (preferably without implementing your own method) to check that a string is parseable with Double.parseDouble()?

like image 380
Louis Rhys Avatar asked Aug 22 '10 22:08

Louis Rhys


People also ask

How do you check if a string can be parsed to double?

To check if the string contains numbers only, in the try block, we use Double 's parseDouble() method to convert the string to a Double . If it throws an error (i.e. NumberFormatException error), it means the string isn't a number and numeric is set to false . Else, it's a number.

What method parses a string to a double value?

parseDouble() We can parse String to double using parseDouble() method.

Can you convert string to double?

We can convert String to double in java using Double. parseDouble() method.


2 Answers

Apache, as usual, has a good answer from Apache Commons-Lang in the form of NumberUtils.isCreatable(String).

Handles nulls, no try/catch block required.

like image 112
bluedevil2k Avatar answered Oct 17 '22 04:10

bluedevil2k


You can always wrap Double.parseDouble() in a try catch block.

try {   Double.parseDouble(number); } catch(NumberFormatException e) {   //not a double } 
like image 45
jdc0589 Avatar answered Oct 17 '22 05:10

jdc0589