Android String to Float Convert isFloat(String str, int precision)

Here you can find the source of isFloat(String str, int precision)

Description

is Float

Declaration

public static boolean isFloat(String str, int precision) 

Method Source Code

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static boolean isFloat(String str) {
        if (isLong(str)) {
            return true;
        }/*  w  ww. j  av  a 2s . c  o m*/
        Pattern pattern = Pattern.compile("\\d*\\.{1}\\d+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean isFloat(String str, int precision) {
        String regStr = "\\d*\\.{1}\\d{" + precision + "}";
        Pattern pattern = Pattern.compile(regStr);
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean isLong(String str) {
        if ("0".equals(str.trim())) {
            return true;
        }
        Pattern pattern = Pattern.compile("^[^0]\\d*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}

Related

  1. String2AmountFloat(String str)
  2. isFloat(String str)
  3. getFloatValue(double val, int len)
  4. parseFloat(String string, float defValue)
  5. TryParse(String str, float defaultFloat)
  6. parseFloat(String value)