StringUtils.isNumeric returns true for "" and false for 7.8. This is of course it's documented behavior, but really not the most convenient for me. Is there something else (ideally in commons.lang) that provides an isActuallyNumeric?
-
in 3.2.1 version, this method returns false for empty string – Line Sep 13 '19 at 10:03
4 Answers
Try isNumber(String)
from org.apache.commons.lang.math.NumberUtils
.
Checks whether the String [is] a valid Java number.
Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
Null
and empty String will returnfalse
.
UPDATE -
isNumber(String)
is now deprecated. Use isCreatable(String)
instead.
Thank you eav for pointing it out.

- 7,761
- 2
- 29
- 53
-
1some strings like "5.f" or "5.d" will register as numbers (I'm guessing having something to do with double or float notation), but others like "5.x" won't. – dstrube Jun 01 '12 at 16:11
-
This is slightly better. Unfortunately: '**+2**' turns out to be NOT isNumber(). – will Sep 17 '14 at 01:59
-
2
-
2Update: isNumber(String) is deprecated in commons-lang3 3.5, I'm using isParseable(String), which works fine - thanks for the hint. – Gunnar May 31 '17 at 09:37
-
Just jumped over this answer, keep in mind that: ```@deprecated This feature will be removed in Lang 4.0, use {@link NumberUtils#isCreatable(String)} instead``` – eav Aug 17 '17 at 12:15
NumberUtils.isNumber is deprecated.
Your should use
NumberUtils.isCreatable
or
NumberUtils.isParsable
All of them support a decimal point value:
- NumberUtils.isCreatable support hexadecimal, octal numbers, scientific notation and numbers marked with a type qualifier (e.g. 123L). Not valid octal values will return
false
(e.g. 09). If you want get its value, you should use NumberUtils.createNumber. - NumberUtils.isParsable only support
0~9
and decimal point (.
), any other character (e.g. space or other anything) will returnfalse
.
By the way, StringUtils.isNumeric
's implement has some different between commons-lang and commons-lang3. In commons-lang, StringUtils.isNumeric("") is true
. But in commons-lang3, StringUtils.isNumeric("") is false
. You can get more info by documents.

- 8,444
- 2
- 30
- 32

- 1,123
- 8
- 11
This isn't exactly in commons.lang
, but it will work.
try {
double d = Double.parseDouble(string);
// string is a number
} catch (NumberFormatException e) {
// string is not a number
}

- 44,417
- 8
- 90
- 141
-
1This solution assumes you're only going for base-10 numbers, within the domain of doubles. Which isn't a bad assumption most of the time, and this solution doesn't create any new dependencies. +1 – May 31 '12 at 01:13
alternatively you can check to see if any character matches a non Digit like this..
if(myStr.replaceAll("^$"," ").matches("[^\\d\\.]"))
then you know there's stuff in there that isn't 0-9 and/or a .
Here's the javascript equivalent (modify the string to experiment)...

- 27,946
- 39
- 137
- 236
-
2The regex won't find (and reject) a string with multiple decimal points, such as "123.9.1" – May 31 '12 at 00:18
-
I think you mean "[^\\D\\.]" ... otherwise you'll have a bad escape character sequence. – Tim Bender May 31 '12 at 00:18
-
-
What if the number has that optional sign symbol? Like +42.1 or -16.4 – Tim Bender May 31 '12 at 00:19
-
well, you can include whatever characters you wish not to look for like - and +, since this isn't an actual numeric evaluator, it's entirely up to you what you feel qualifies as a valid number character. Europeans for example use , as their decimal point. – Yevgeny Simkin May 31 '12 at 00:20
-
@bdares, ok... I guess you'd need to add an additional validation to make sure someone hasn't included multiple decimal places – Yevgeny Simkin May 31 '12 at 00:23
-
I'm going to throw a +1 in anyway, because I don't like the idea of including a large library like apache for some simple operations, but I think this is one of those cases where trying to come up with the right regex is probably not worth the effort. – Tim Bender May 31 '12 at 00:25
-
@Tim Bender, yeah... it really depends on the circumstances. If you're just looking to verify that something is a number and not worried that someone is desperately trying to outwit your input, then this regex will work fine. At some point, idiot proofing takes its toll:) – Yevgeny Simkin May 31 '12 at 00:27