Java Utililty Methods Number Parse

List of utility methods to do Number Parse

Description

The list of methods to do Number Parse are organized into topic(s).

Method

NumbertoNumber(CharSequence jsonText)
To number
Number value = null;
if (value == null)
    try {
        value = Integer.parseInt(jsonText.toString());
    } catch (NumberFormatException e) {
if (value == null)
    try {
...
NumbertoNumber(final Object obj)
to Number
try {
    if (obj instanceof String) {
        return Double.valueOf((String) obj);
    } else if (obj instanceof Number) {
        return (Number) obj;
    } else {
        throw new IllegalArgumentException("Could not convert value to number: " + obj);
} catch (Exception e) {
    throw new IllegalArgumentException("Could not convert value to number: " + obj, e);
NumbertoNumber(Object object, Number defaultValue)
Extract the value represented by the given object (Number or String) - trialing '.0*' will be removed.
if (object == null) {
    return defaultValue;
if (object instanceof Number) {
    return (Number) object;
String s = object.toString();
s = s.replaceAll("\\.0*$", ""); 
...
NumbertoNumber(Object object, Number defaultValue)
to Number
if (object == null) {
    return defaultValue;
if (object instanceof Number) {
    return (Number) object;
String s = object.toString();
s = s.replaceAll("\\.0*$", "");
...
LongtoNumber(String hash)
Decodes the String provided which should represent a hexadecimal number into a Long .
return (hash == null) ? null : Long.decode("0x" + hash);
NumbertoNumber(String num)
to Number
if (num == null || num.length() < 1)
    return null;
if (num.contains(".")) {
    return Double.parseDouble(num);
} else {
    return Integer.parseInt(num);
inttoNumber(String s)
to Number
return Integer.valueOf(s);
StringtoNumber(String string)
to Number
char[] stringArray = string.toCharArray();
StringBuilder number = new StringBuilder();
for (char ch : stringArray) {
    if (ch >= '0' && ch <= '9') {
        number.append(ch);
return number.toString();
...
NumbertoNumber(String text)
Parses a JSON formatted number from text.
if (text == null) {
    return null;
boolean fp = false;
for (char c : text.toCharArray()) {
    if (c == '.' || c == 'e' || c == 'E') {
        fp = true;
        break;
...
inttoNumber(String value, int defValue)
Convenience method for converting a String into an Integer.
if (value != null) {
    try {
        return Integer.parseInt(value);
    } catch (NumberFormatException e) {
return defValue;