版权声明:lpf https://blog.csdn.net/justlpf/article/details/83617658
package org.fiend.basetest;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;
/**
* 判断字符串是否为正负整数或浮点数
* @author Administrator 2018/10/31
*/
public class NumberJudge {
public static void main(String[] args) {
// System.out.println(isPosOrNegIntegerOrFloat("-1230.0"));
// System.out.println(isPosOrNegFloat("+12300.4"));
System.out.println(isPosOrNegInteger("-11"));
// System.out.println(isPosInteger("0"));
}
/**
* use
* 判断是否为整数或浮点数
*/
static boolean isPosOrNegIntegerOrFloat(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?(0|[1-9]\\d*)(\\.\\d+)?$");
boolean rst = pattern.matcher(str).matches();
return rst;
}
/**
* use
* 是否为浮点数
*/
static boolean isPosOrNegFloat(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+[.{1}][\\d]+$");
boolean rst = pattern.matcher(str).matches();
return rst;
}
/**
* use
* 判断是否为正/负整数
*/
static boolean isPosOrNegInteger(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
boolean rst = pattern.matcher(str).matches();
return rst;
}
}