【commons-lang3专题】004- NumberUtils 专题

267 阅读5分钟

【commons-lang3专题】004- NumberUtils 专题

[toc]

〇、准备

1、NumberUtils 主要作用

提供生成各种数字操作方法。

2、引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

一、常用常量

1、Long 值

// 1、Long 值
System.out.println(NumberUtils.LONG_ZERO); // 0
System.out.println(NumberUtils.LONG_ONE); // 1
System.out.println(NumberUtils.LONG_MINUS_ONE); // -1
System.out.println(NumberUtils.LONG_INT_MIN_VALUE); // -2147483648
System.out.println(NumberUtils.LONG_INT_MAX_VALUE); // 2147483647

2、Integer 值

// 2、Integer 值
System.out.println(NumberUtils.INTEGER_ZERO); // 0
System.out.println(NumberUtils.INTEGER_ONE); // 1
System.out.println(NumberUtils.INTEGER_TWO); // 2
System.out.println(NumberUtils.INTEGER_MINUS_ONE); // -1

3、Short 值

// 3、Short 值
System.out.println(NumberUtils.SHORT_ZERO); // 0
System.out.println(NumberUtils.SHORT_ONE); // 1
System.out.println(NumberUtils.SHORT_MINUS_ONE); // -1

二、比较大小

4、比较大小

// 4、比较大小
System.out.println(NumberUtils.compare(1, 10)); // -1
System.out.println(NumberUtils.compare(1L, 10L)); // -1
System.out.println(NumberUtils.compare('A', 'B')); // -1

三、字符串转 BigInteger 和 BigInteger

5、字符串转 BigInteger 对象

// 5、字符串转 BigInteger 对象
// 说明:1、将字符串转成 BigInteger,支持 10进制,十六进制(以0X或者#开头)、8进制(以0开头)
// 说明:2、如果 str 为 null,则返回 null。如果转换错误,则抛出 NumberFormatException 异常
System.out.println(NumberUtils.createBigInteger("1000000000")); // 1000000000

6、字符串转 BigInteger 对象

// 6、字符串转 BigInteger 对象
// 说明:1、将字符串转成 BigDecimal,只支持 10 进制,底层就是使用 new BigDecimal(str)
// 说明:2、str 为null时,返回 null,str 为空时会抛出 NumberFormatException 异常
System.out.println(NumberUtils.createBigDecimal("1000000000")); // 1000000000

四、字符串转数字包装类

7、字符串转 Number 对象

// 7、字符串转 Number 对象
System.out.println(NumberUtils.createNumber("100")); // 100

8、字符串转 Integer 对象

// 8、字符串转 Integer 对象
System.out.println(NumberUtils.createInteger("100")); // 100

9、字符串转 Float 对象

// 9、字符串转 Float 对象
System.out.println(NumberUtils.createFloat("1.1")); // 1.1

10、字符串转 Double 对象

// 10、字符串转 Double 对象
System.out.println(NumberUtils.createDouble("1.1")); // 1.1

五、判断字符串是否和数字有关系

11、判断字符串是否是有效的数字

// 11、判断字符串是否是有效的数字
// 支持:16进制、8进制、10进制、正数负数、科学计数法(如8.788006e+05)、类型限定符(110L、3.14f)
// str 为空或者为 null,都返回 false
System.out.println(NumberUtils.isCreatable("100")); // true
System.out.println(NumberUtils.isCreatable("0XFF")); // true
System.out.println(NumberUtils.isCreatable("-1")); // true
System.out.println(NumberUtils.isCreatable("8.788006e+05")); // true
System.out.println(NumberUtils.isCreatable("110L")); // true

12、判断字符串是否只含有数字

// 12、判断字符串是否只含有数字
// 检查字符串中是否只含有数字,负数和小数都会返回 false
// str 为 null 或者为空 返回 false
System.out.println(NumberUtils.isDigits("100")); // true
System.out.println(NumberUtils.isDigits("hello")); // false
System.out.println(NumberUtils.isDigits("")); // false
System.out.println(NumberUtils.isDigits(null)); // false
System.out.println(NumberUtils.isDigits("hello100world")); // false

13、判断字符串是否可以解析为数字

// 13、判断字符串是否可以解析为数字
// 说明:只支持 10 进制,支持正负数,支持小数,支持不 8进制、16进制、不支持科学计数法,也不支持类型限定符(如 3000L,3.14F)
System.out.println(NumberUtils.isParsable("100")); // true
System.out.println(NumberUtils.isParsable("100.1")); // true
System.out.println(NumberUtils.isParsable("-1")); // true
System.out.println(NumberUtils.isParsable("8.788006e+05")); // false
System.out.println(NumberUtils.isParsable("3000L")); // false
System.out.println(NumberUtils.isParsable("hello100world")); // false

六、取极值

14、求最大值

// 14、求最大值
// 支持:byte、double、float、int、long、short
System.out.println(NumberUtils.max(1, 2, 3)); // 3

15、求最小值

// 15、求最小值
// 支持:byte、double、float、int、long、short
System.out.println(NumberUtils.min(1, 2, 3)); // 1

七、字符串转基本数据类型

16、字符串转 byte

// 16、字符串转 byte
// 字符串转 byte,如果转换失败,异常捕获后不会抛出,直接返回默认值0;如果 str 为 null,则返回默认值 0
System.out.println(NumberUtils.toByte("10")); // 10
System.out.println(NumberUtils.toByte("hello", (byte) 1)); // 1

17、字符串转 double

// 17、字符串转 double
System.out.println(NumberUtils.toDouble("1.1")); // 1.1
System.out.println(NumberUtils.toDouble("hello", 2L)); // 2.0
System.out.println(NumberUtils.toDouble(new BigDecimal("1000"))); // 1000.0
System.out.println(NumberUtils.toDouble(new BigDecimal("2"), 1L)); // 2.0

18、字符串转 float

// 18、字符串转 float
System.out.println(NumberUtils.toFloat("1.1")); // 1.1
System.out.println(NumberUtils.toFloat("hello", 2.2F)); // 2.2

19、字符串转 int

// 19、字符串转 int
System.out.println(NumberUtils.toInt("1")); // 1
System.out.println(NumberUtils.toInt("hello", 2)); // 2

20、字符串转 long

// 20、字符串转 long
System.out.println(NumberUtils.toLong("1")); // 1
System.out.println(NumberUtils.toLong("hello", 2L)); // 2

21、字符串转 short

// 21、字符串转 short
System.out.println(NumberUtils.toShort("1")); // 1
System.out.println(NumberUtils.toShort("hello", (short) 2)); // 2

八、完整代码

package com.zibo.zibo2022.number_utils.main;

import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) {
        // start
        // 一、常用常量
        // 1、Long 值
        System.out.println(NumberUtils.LONG_ZERO); // 0
        System.out.println(NumberUtils.LONG_ONE); // 1
        System.out.println(NumberUtils.LONG_MINUS_ONE); // -1
        System.out.println(NumberUtils.LONG_INT_MIN_VALUE); // -2147483648
        System.out.println(NumberUtils.LONG_INT_MAX_VALUE); // 2147483647

        // 2、Integer 值
        System.out.println(NumberUtils.INTEGER_ZERO); // 0
        System.out.println(NumberUtils.INTEGER_ONE); // 1
        System.out.println(NumberUtils.INTEGER_TWO); // 2
        System.out.println(NumberUtils.INTEGER_MINUS_ONE); // -1

        // 3、Short 值
        System.out.println(NumberUtils.SHORT_ZERO); // 0
        System.out.println(NumberUtils.SHORT_ONE); // 1
        System.out.println(NumberUtils.SHORT_MINUS_ONE); // -1

        // 二、比较大小
        // 4、比较大小
        System.out.println(NumberUtils.compare(1, 10)); // -1
        System.out.println(NumberUtils.compare(1L, 10L)); // -1
        System.out.println(NumberUtils.compare('A', 'B')); // -1

        // 三、字符串转 BigInteger 和 BigInteger
        // 5、字符串转 BigInteger 对象
        // 说明:1、将字符串转成 BigInteger,支持 10进制,十六进制(以0X或者#开头)、8进制(以0开头)
        // 说明:2、如果 str 为 null,则返回 null。如果转换错误,则抛出 NumberFormatException 异常
        System.out.println(NumberUtils.createBigInteger("1000000000")); // 1000000000

        // 6、字符串转 BigInteger 对象
        // 说明:1、将字符串转成 BigDecimal,只支持 10 进制,底层就是使用 new BigDecimal(str)
        // 说明:2、str 为null时,返回 null,str 为空时会抛出 NumberFormatException 异常
        System.out.println(NumberUtils.createBigDecimal("1000000000")); // 1000000000

        // 四、字符串转数字
        // 7、字符串转 Number 对象
        System.out.println(NumberUtils.createNumber("100")); // 100

        // 8、字符串转 Integer 对象
        System.out.println(NumberUtils.createInteger("100")); // 100

        // 9、字符串转 Float 对象
        System.out.println(NumberUtils.createFloat("1.1")); // 1.1

        // 10、字符串转 Double 对象
        System.out.println(NumberUtils.createDouble("1.1")); // 1.1

        // 五、判断字符串是否和数字有关系
        // 11、判断字符串是否是有效的数字
        // 支持:16进制、8进制、10进制、正数负数、科学计数法(如8.788006e+05)、类型限定符(110L、3.14f)
        // str 为空或者为 null,都返回 false
        System.out.println(NumberUtils.isCreatable("100")); // true
        System.out.println(NumberUtils.isCreatable("0XFF")); // true
        System.out.println(NumberUtils.isCreatable("-1")); // true
        System.out.println(NumberUtils.isCreatable("8.788006e+05")); // true
        System.out.println(NumberUtils.isCreatable("110L")); // true

        // 12、判断字符串是否只含有数字
        // 检查字符串中是否只含有数字,负数和小数都会返回 false
        // str 为 null 或者为空 返回 false
        System.out.println(NumberUtils.isDigits("100")); // true
        System.out.println(NumberUtils.isDigits("hello")); // false
        System.out.println(NumberUtils.isDigits("")); // false
        System.out.println(NumberUtils.isDigits(null)); // false
        System.out.println(NumberUtils.isDigits("hello100world")); // false

        // 13、判断字符串是否可以解析为数字
        // 说明:只支持 10 进制,支持正负数,支持小数,支持不 8进制、16进制、不支持科学计数法,也不支持类型限定符(如 3000L,3.14F)
        System.out.println(NumberUtils.isParsable("100")); // true
        System.out.println(NumberUtils.isParsable("100.1")); // true
        System.out.println(NumberUtils.isParsable("-1")); // true
        System.out.println(NumberUtils.isParsable("8.788006e+05")); // false
        System.out.println(NumberUtils.isParsable("3000L")); // false
        System.out.println(NumberUtils.isParsable("hello100world")); // false

        // 六、取极值
        // 14、求最大值
        // 支持:byte、double、float、int、long、short
        System.out.println(NumberUtils.max(1, 2, 3)); // 3

        // 15、求最小值
        // 支持:byte、double、float、int、long、short
        System.out.println(NumberUtils.min(1, 2, 3)); // 1

        // 七、字符串转基本数据类型
        // 16、字符串转 byte
        // 字符串转 byte,如果转换失败,异常捕获后不会抛出,直接返回默认值0;如果 str 为 null,则返回默认值 0
        System.out.println(NumberUtils.toByte("10")); // 10
        System.out.println(NumberUtils.toByte("hello", (byte) 1)); // 1

        // 17、字符串转 double
        System.out.println(NumberUtils.toDouble("1.1")); // 1.1
        System.out.println(NumberUtils.toDouble("hello", 2L)); // 2.0
        System.out.println(NumberUtils.toDouble(new BigDecimal("1000"))); // 1000.0
        System.out.println(NumberUtils.toDouble(new BigDecimal("2"), 1L)); // 2.0

        // 18、字符串转 float
        System.out.println(NumberUtils.toFloat("1.1")); // 1.1
        System.out.println(NumberUtils.toFloat("hello", 2.2F)); // 2.2

        // 19、字符串转 int
        System.out.println(NumberUtils.toInt("1")); // 1
        System.out.println(NumberUtils.toInt("hello", 2)); // 2

        // 20、字符串转 long
        System.out.println(NumberUtils.toLong("1")); // 1
        System.out.println(NumberUtils.toLong("hello", 2L)); // 2

        // 21、字符串转 short
        System.out.println(NumberUtils.toShort("1")); // 1
        System.out.println(NumberUtils.toShort("hello", (short) 2)); // 2
        // end
    }

}