What is NumberUtils.isCreatable in Java?
isCreatable() is a NumberUtils class that is used to check whether a given string is a valid Java number or not. A null, empty, or blank String will return false.
Import NumberUtils
The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
You can import the NumberUtils class as follows:
import org.apache.commons.lang3.math.NumberUtils;
Syntax
public static boolean isCreatable(final String str)
Parameters
final String str: The string to check.
Return value
This method returns true if the string is a valid number in Java. Otherwise, it returns false.
Code
import org.apache.commons.lang3.math.NumberUtils;public class Main{public static void main(String[] args){// Example 1String valueToConvert = "234.323232232";System.out.printf("NumberUtils.isCreatable(%s) = %s", valueToConvert, NumberUtils.isCreatable(valueToConvert));System.out.println();// Example 2valueToConvert = "0xABED";System.out.printf("NumberUtils.isCreatable(%s) = %s", valueToConvert, NumberUtils.isCreatable(valueToConvert));System.out.println();// Example 3valueToConvert = "q3ewsf";System.out.printf("NumberUtils.isCreatable(%s) = %s", valueToConvert, NumberUtils.isCreatable(valueToConvert));System.out.println();}}
Example 1
- string =
234.323232232
The method returns true because the string is a valid decimal value.
Example 2
- string =
0xABED
The method returns true because the string is a valid hexadecimal value.
Example 3
- string =
q3ewsf
The method returns false because the string is cannot be converted to a valid number in java.
Output
The output of the code will be as follows:
NumberUtils.isCreatable(234.323232232) = true
NumberUtils.isCreatable(0xABED) = true
NumberUtils.isCreatable(q3ewsf) = false
Free Resources
- undefined by undefined
- undefined by undefined