How can I check if a string is a valid number?

2nd October 2020: note that many bare-bones approaches are fraught with subtle bugs (eg. whitespace, implicit partial parsing, radix, coercion of arrays etc.) that many of the answers here fail to take into account. The following implementation might work for you, but note that it does not cater for number separators other than the decimal … Read more

How to check if a String is numeric in Java

With Apache Commons Lang 3.5 and above: NumberUtils.isCreatable or StringUtils.isNumeric. With Apache Commons Lang 3.4 and below: NumberUtils.isNumber or StringUtils.isNumeric. You can also use StringUtils.isNumericSpace which returns true for empty strings and ignores internal spaces in the string. Another way is to use NumberUtils.isParsable which basically checks the number is parsable according to Java. (The … Read more

IsNumeric function in c#

I know it’s possible to check whether the value of a text box or variable is numeric using try/catch statements, but IsNumeric is so much simpler. One of my current projects requires recovering values from text boxes. Unfortunately, it is written in C#. I understand that there’s a way to enable the Visual Basic IsNumeric … Read more

How can I compare numbers in Bash?

In Bash, you should do your check in an arithmetic context: For POSIX shells that don’t support (()), you can use -lt and -gt. You can get a full list of comparison operators with help test or man test.