Is there a native way (preferably without implementing your own method) to check that a string is parseable with Double.parseDouble()
?
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.
parseDouble() We can parse String to double using parseDouble() method.
We can convert String to double in java using Double. parseDouble() method.
Apache
, as usual, has a good answer from Apache Commons-Lang
in the form of NumberUtils.isCreatable(String)
.
Handles null
s, no try
/catch
block required.
You can always wrap Double.parseDouble() in a try catch block.
try { Double.parseDouble(number); } catch(NumberFormatException e) { //not a double }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With