Most elegant isNumeric() solution for java
Asked Answered
D

7

16

I'm porting a small snippet of PHP code to java right now, and I was relying on the function is_numeric($x) to determine if $x is a number or not. There doesn't seem to be an equivalent function in java, and I'm not satisfied with the current solutions I've found so far.

I'm leaning toward the regular expression solution found here: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric

Which method should I use and why?

Donohoe answered 17/8, 2010 at 21:35 Comment(2)
What do you mean exactly by numeric? Do you want the PHP definition exactly? php.net/manual/en/function.is-numeric.phpVillainy
Doesn't have to match the PHP definition exactly, but it would be great if it did since I know my algorithm works with that function.Donohoe
S
16

Note that the PHP isNumeric() function will correctly determine that hex and scientific notation are numbers, which the regex approach you link to will not.

One option, especially if you are already using Apache Commons libraries, is to use NumberUtils.isNumber(), from Commons-Lang. It will handle the same cases that the PHP function will handle.

Snazzy answered 17/8, 2010 at 21:45 Comment(3)
That function does appear to do exactly what I want, but I'm not using the apache commons library :(Donohoe
@Donohoe Commons lang exists in pretty much every java program, it helps in so many ways. Download it and add it to your classpath, you will be glad you didPahari
It's always tricky to decide whether it's justified to add a library when you only need one little piece of it. But it's likely that, if you look into it, you'll find that Apache Commons Lang can give you other things you would use -- it's basically "the stuff that ought to be built in to Java but isn't". I strongly recommend The Common Java Cookbook (discursive.com/books/cjcook/reference/book-cjcook.html) which gives a lot of tips for using Apache libraries.Snazzy
J
4

Have you looked into using StringUtils library? There's a isNumeric() function which might be what you're looking for. (Note that "" would be evaluated to true)

Jawbone answered 17/8, 2010 at 21:44 Comment(2)
The Commons isNumeric function only looks for integers -- it will reject, for example, a number containing a decimal point, and certainly doesn't accept scientific notation. See my answer for a different Commons function that seems closer to what you are looking for.Snazzy
@JacobM: indeed, I saw this NumberUtils and was on to update my answer when I saw your comment.Jawbone
F
2

It's usually a bad idea to have a number in a String. If you want to use this number then parse it and use it as a numeric. You shouldn't need to "check" if it's a numeric, either you want to use it as a numeric or not.

If you need to convert it, then you can use every parser from Integer.parseInt(String) to BigDecimal(String)

If you just need to check that the content can be seen as a numeric then you can get away with regular expressions.

And don't use the parseInt if your string can contain a float.

Faultfinder answered 17/8, 2010 at 21:41 Comment(1)
Usually, if someone is looking for an isNumeric method, they bloody well didn't put the poor number into the string themselves ;)Oreilly
A
2

Optionally you can use a regular expression as well.

   if (theString.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")))
     return true;

    return false;
Aceydeucy answered 17/8, 2010 at 21:57 Comment(1)
What is with that last +? "-7.0-4.2" will return true to this expression.Donohoe
B
1

Did you try Integer.parseInt()? (I'm not sure of the method name, but the Integer class has a method that creates an Integer object from strings). Or if you need to handle non-integer numbers, similar methods are available for Double objects as well. If these fail, an exception is thrown.

If you need to parse very large numbers (larger than int/double), and don't need the exact value, then a simple regex based method might be sufficient.

Blader answered 17/8, 2010 at 21:39 Comment(2)
If you expect to have a reasonable proportion of strings that are not numeric, using a thrown exception to find them is both bad practice (a non-numeric string isn't "exceptional") and likely to cause performance issues.Snazzy
Thats true. It depends on the usage scenarios.Nabal
B
0

In a strongly typed language, a generic isNumeric(String num) method is not very useful. 13214384348934918434441 is numeric, but won't fit in most types. Many of those where is does fit won't return the same value.

As Colin has noted, carrying numbers in Strings withing the application is not recommended. The isNumberic function should only be applicable for input data on interface methods. These should have a more precise definition than isNumeric. Others have provided various solutions. Regular expressions can be used to test a number of conditions at once, including String length.

Blip answered 17/8, 2010 at 22:17 Comment(0)
S
0

Just use if((x instanceof Number)

//if checking for parsable number also

|| (x instanceof String && x.matches("((-|\+)?[0-9]+(\.[0-9]+)?)+"))

){ ... }
//---All numeric types including BigDecimal extend Number

Starch answered 23/11, 2013 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.