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

- 18,541
- 27
- 119
- 168

- 34,517
- 56
- 153
- 221
6 Answers
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.

- 17,329
- 10
- 113
- 185

- 9,366
- 8
- 43
- 57
-
does Apache ship with the normal JDK? – Louis Rhys Aug 23 '10 at 01:44
-
5No. You can find the Apache Commons libraries here. http://commons.apache.org/ Highly recommended that you use them rather than writing custom code whenever you can. – Chris Nava Aug 23 '10 at 02:14
-
1Does this identify decimal numbers ? – TechCrunch Sep 16 '13 at 17:09
-
8It returns false for float/double numbers. – Constantine Gladky May 30 '14 at 09:49
-
This one does not work for certain numbers. For instance "3L". Use the accepted answer instead. For a quick example see https://gist.github.com/nachtfisch/945e3df75d70b10de99d – Christian F Oct 27 '15 at 17:18
-
Using `commons/lang3/math/NumberUtils, it returns `true` for input strings `1`, `-1.23e5` or `5L`. It looks fine – tokland Mar 31 '16 at 09:36
-
2`NumberUtils.isNumber(String)` has been deprecated, you should now use `NumberUtils.isCreatable(String)` instead. – Sakiboy Dec 23 '16 at 18:40
-
The question is about a double, while this would return true for any kind of number, which isn't necessarily a double. – Asu May 24 '17 at 14:09
-
All numbers are doubles, so if it passes the number test, it will pass the double test. – bluedevil2k Jun 02 '17 at 20:12
You can always wrap Double.parseDouble() in a try catch block.
try
{
Double.parseDouble(number);
}
catch(NumberFormatException e)
{
//not a double
}

- 6,972
- 5
- 36
- 39
-
10This would be my preferred method because your guaranteed to get exactly the same behavior as the parse. It's very easy with a custom regular expression to overlook edge cases. – Chris Nava Aug 23 '10 at 02:17
-
2This is good EXCEPT if you type in a number followed by a space, it doesn't catch it. – giant91 Sep 06 '13 at 05:03
-
3
-
2@giant91 So? The string is parseable, which is the actual question. – user207421 Jan 27 '17 at 00:55
-
2I like this better than the accepted answer. This is guaranteed to work in all future versions of Java, whereas using RegEx tries to be smart and anticipate parser behavior. Regex are very slow anyway, it's not like trying to find out the string is suitable will save you much time over simply trying to parse it and going through the exception-catching process. – Gabriel Magana Jan 08 '19 at 18:54
The common approach would be to check it with a regular expression like it's also suggested inside the Double.valueOf(String)
documentation.
The regexp provided there (or included below) should cover all valid floating point cases, so you don't need to fiddle with it, since you will eventually miss out on some of the finer points.
If you don't want to do that, try catch
is still an option.
The regexp suggested by the JavaDoc is included below:
final String Digits = "(\\p{Digit}+)";
final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
final String Exp = "[eE][+-]?"+Digits;
final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from the Java Language Specification, 2nd
// edition, section 3.10.2.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
if (Pattern.matches(fpRegex, myString)){
Double.valueOf(myString); // Will not throw NumberFormatException
} else {
// Perform suitable alternative action
}

- 18,541
- 27
- 119
- 168

- 2,715
- 17
- 17
-
-
-
2@Louis Rhys - There is a code example in the documentation link in the answer. – Starkey Aug 22 '10 at 22:44
-
1@Louis Rhys: The complete code you need is provided in behind the link. You just need to copy that into your code and wrap the final if statement around your use of `Double.parseDouble`. But I would put the could into a utility class or method so it's easier reusable in your project. Something like `parseDoubleSafe(String check, Double default)` or something like that. – Johannes Wachter Aug 22 '10 at 22:45
-
4the approach taken by the Java API developer is imbecile: they gives us a chunk of code that every interested person has to copy/paste into a utility method. Why not just providing that method themselves in the `Double` class? :facepalm (endless) – Clint Eastwood Jun 09 '20 at 20:43
Something like below should suffice :-
String decimalPattern = "([0-9]*)\\.([0-9]*)";
String number="20.00";
boolean match = Pattern.matches(decimalPattern, number);
System.out.println(match); //if true then decimal else not

- 20,654
- 10
- 86
- 101
-
3actually it's more complicated than that. see johannes wachter's answer – Louis Rhys Aug 22 '10 at 22:47
-
1
-
Hi, this is more proper for doubles: String doublePattern = "([0-9].*)\\.([0-9].*)"; – voodoo98 May 07 '15 at 09:32
-
2I find this answer very good. It is much more flexible than the others and man is not dependent on a third party library. – Ad Infinitum Aug 28 '17 at 10:27
-
1Late to the party, but this also matches a dot as a valid double, while it would not match "20" as valid, which it actually is. – Taco Jan Osinga Jan 25 '21 at 11:42
Google's Guava library provides a nice helper method to do this: Doubles.tryParse(String)
. You use it like Double.parseDouble
but it returns null
rather than throwing an exception if the string does not parse to a double.

- 1,793
- 5
- 28
- 34
All answers are OK, depending on how academic you want to be. If you wish to follow the Java specifications accurately, use the following:
private static final Pattern DOUBLE_PATTERN = Pattern.compile(
"[\\x00-\\x20]*[+-]?(NaN|Infinity|((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)" +
"([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|" +
"(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))" +
"[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
public static boolean isFloat(String s)
{
return DOUBLE_PATTERN.matcher(s).matches();
}
This code is based on the JavaDocs at Double.

- 2,127
- 18
- 12