Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to check if a String can be parsed to Double in Java

I know there's a million ways of doing this but what is the fastest? This should include scientific notation.

NOTE: I'm not interested in converting the value to Double, i'm only interested in knowing if it's possible. i.e. private boolean isDouble(String value).

like image 820
JHollanti Avatar asked Dec 19 '11 17:12

JHollanti


People also ask

Is there a parse double in Java?

The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double. Parameters: It accepts a single mandatory parameter s which specifies the string to be parsed.


1 Answers

There is a handy NumberUtils#isNumber in Apache Commons Lang. It is a bit far fetched:

Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).

but I guess it might be faster than regular expressions or throwing and catching an exception.

like image 70
Tomasz Nurkiewicz Avatar answered Nov 15 '22 00:11

Tomasz Nurkiewicz