Android String to Number Convert isNumeric(String str)

Here you can find the source of isNumeric(String str)

Description

is Numeric

Declaration

public static boolean isNumeric(String str) 

Method Source Code

//package com.java2s;

public class Main {
    public static boolean isNumeric(String str) {
        int index = str.indexOf(".");
        if (index == -1) {
            return isIntegral(str);
        }/*from w  w w  .  j  a va  2 s .  c  o  m*/
        if ((index == 0) || (index == str.length() - 1)) {
            return false;
        }
        String num1 = str.substring(0, index);
        String num2 = str.substring(index + 1);
        return (isIntegral(num1)) && (isAllCharDigit(num2));
    }

    public static boolean isIntegral(String str) {
        if (str.startsWith("-")) {
            if (str.length() == 1) {
                return false;
            }
            str = str.substring(1);
        }
        if ((str.startsWith("0")) && (str.length() > 1)) {
            return false;
        }
        return isAllCharDigit(str);
    }

    public static boolean isAllCharDigit(String str) {
        int length = str.length();
        if (length == 0) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. isNumeric(String str)
  2. isNumeric(String str)
  3. isNumeric(String str)
  4. isNumeric(String string)
  5. isLetterNumeric(String s)
  6. isNumericOnly(String pString)