java 数值判断工具类

简介: java 数值判断工具类

public class NumberUtil {

private static Pattern patternIsNumeric = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");

/**
 * 判断字符串是否为数字,包括小数点
 *
 * @param str
 * @return
 */
public static boolean isNumberic(String str) {
    if (str == null) {
        return false;
    }
    if (str.length() == 0) {
        return false;
    }
    String bigStr;
    try {
        bigStr = new BigDecimal(str).toString();
    } catch (Exception e) {
        //异常 说明包含非数字。
        return false;
    }
    Matcher isNum = patternIsNumeric.matcher(str);
    if (!isNum.matches()) {
        return false;
    }
    return true;
}

/**
 * 判断Integer 是否为null
 *
 * @param str
 * @return
 */
public static boolean isIntegerNull(Integer str) {
    return str == null;
}

/**
 * 判断Integer 是否不为null
 *
 * @param str
 * @return
 */
public static boolean isIntegerNotNull(Integer str) {
    return str != null;
}

/**
 * 判断Integer 是否不为null 并且 大于0
 *
 * @param str
 * @return
 */
public static boolean isIntegerNotNullAndGreaterThan0(Integer str) {
    if (str == null) {
        return false;
    }
    return str > 0;
}

/**
 * 判断 Long 是否为null
 *
 * @param str
 * @return
 */
public static boolean isLongNull(Long str) {
    return str == null;
}

/**
 * 判断 Long 是否不为null
 *
 * @param str
 * @return
 */
public static boolean isLongNotNull(Long str) {
    return str != null;
}

/**
 * 判断 Long 是否不为null
 *
 * @param str
 * @return
 */
public static boolean isLongNotNullAndGreaterThan0(Long str) {
    if (str == null) {
        return false;
    }
    return str > 0;
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @param value 默认值
 * @return
 */
public static Integer toInt(Integer str, Integer value) {
    if (isIntegerNull(str)) {
        return value;
    }
    return str;
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Integer toInt(Integer str) {
    return toInt(str, 0);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Integer toInt(Long str) {
    return toInt(str.intValue(), 0);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Integer toInt(Double str) {
    return toInt(str.intValue(), 0);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Integer toInt(Float str) {
    return toInt(str.intValue(), 0);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @param value 默认值
 * @return
 */
public static Long toLong(Long str, Long value) {
    if (isLongNull(str)) {
        return value;
    }
    return str;
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Long toLong(Long str) {
    return toLong(str, 0L);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Long toLong(Integer str) {
    return toLong(str.longValue(), 0L);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Long toLong(Double str) {
    return toLong(str.longValue(), 0L);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @return
 */
public static Long toLong(Float str) {
    return toLong(str.longValue(), 0L);
}

/**
 * 判断Integer[] 是否为null
 *
 * @param str
 * @return
 */
public static boolean isArrNotNull(Integer[] str) {
    return str != null && str.length > 0;
}

/**
 * 判断 Long[] 是否为null
 *
 * @param str
 * @return
 */
public static boolean isArrNotNull(Long[] str) {
    return str != null && str.length > 0;
}

/**
 * 转换 long 如果不存在,在使用默认值
 *
 * @param str
 * @param defaultValue
 * @return
 */
public static long toLong(String str, long defaultValue) {
    return NumberUtils.toLong(str, defaultValue);
}

/**
 * 转换 long 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static long toLong(String str) {
    return NumberUtils.toLong(str, 0L);
}

/**
 * 转换 long 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static long toLong(int str) {
    return (long) str;
}

/**
 * 转换 long 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static long toLong(float str) {
    return (long) str;
}

/**
 * 转换 long 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static long toLong(double str) {
    return (long) str;
}

/**
 * 转换 long 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static long toLong(BigDecimal str) {
    return NumberUtils.toLong(str.toString(), 0L);
}


/**
 * 转换 int 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static int toInt(String str) {
    return NumberUtils.toInt(str, 0);
}

/**
 * 转换 int 如果不存在,在使用默认值
 *
 * @param str
 * @return
 */
public static int toInt(String str, int defaultValue) {
    return NumberUtils.toInt(str, defaultValue);
}

/**
 * 转换 int 如果不存在,在使用默认值
 *
 * @param str
 * @return
 */
public static int toInt(BigDecimal str) {
    return NumberUtils.toInt(str.toString(), 0);
}

/**
 * 转换 int 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static int toInt(long str) {
    return (int) str;
}

/**
 * 转换 int 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static int toInt(float str) {
    return (int) str;
}

/**
 * 转换 int 如果不存在,在使用默认值 0
 *
 * @param str
 * @return
 */
public static int toInt(double str) {
    return (int) str;
}

/**
 * 判断 Float 是否为null
 *
 * @param str
 * @return
 */
public static boolean isFloatNull(Float str) {
    return str == null;
}

/**
 * 判断 Double 是否为null
 *
 * @param str
 * @return
 */
public static boolean isDoubleNull(Double str) {
    return str == null;
}

/**
 * 判断 Float 是否不为null 并且 大于0
 *
 * @param str
 * @return
 */
public static boolean isFloatNotNullAndGreaterThan0(Float str) {
    if (str == null) {
        return false;
    }
    return str > 0;
}

/**
 * 判断 Float 是否不为null 并且 大于0
 *
 * @param str
 * @return
 */
public static boolean isDoubleNotNullAndGreaterThan0(Double str) {
    if (str == null) {
        return false;
    }
    return str > 0;
}

/**
 * 判断 Float 是否为null
 *
 * @param str
 * @return
 */
public static boolean isFloatNotNull(Float str) {
    return str != null;
}

/**
 * 判断 Double 是否为null
 *
 * @param str
 * @return
 */
public static boolean isDoubleNotNull(Double str) {
    return str != null;
}

}

相关文章
|
10天前
|
算法 搜索推荐 Java
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
这篇文章介绍了如何使用Java后端技术,结合Graphics2D和Echarts等工具,生成包含个性化信息和图表的海报,并提供了详细的代码实现和GitHub项目链接。
30 0
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
|
15天前
|
Java
Java 些许公共工具类
Java 些许公共工具类
12 1
|
2月前
|
缓存 前端开发 Java
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
【8月更文挑战第10天】java基础巩固,工具类的封装
24 1
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
|
2月前
|
Java
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
|
2月前
|
存储 设计模式 安全
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
38 0
|
3月前
|
设计模式 存储 安全
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
57 1
|
3月前
|
安全 Java 开发者
Java中的并发工具类与线程安全实现
Java中的并发工具类与线程安全实现
|
4月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
3月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
3月前
|
并行计算 Java API
Java中的并发工具类详解
Java中的并发工具类详解