在 Java 中提供了大数字的操作类,即 java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算,其中 BigInteger 类是针对大整数的处理类,而 BigDecimal 类则是针对大小数的处理类。

BigInteger

BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。Integer 是 int 的包装类,所以它的最大值其实和 int 一样,为 int java 最大取值 java int最大值和integer_BigInteger,如果要计算更大的数字,使用 Integer 数据类型就无法实现了,所以 Java 中提供了 BigInteger 来处理更大的数字。BigInteger 支持任意精度的整数,也就是说,在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失信息。在 BigInteger 类中封装了多种操作,除了基本的加减乘除之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数的操作。使用 BigInteger 类时,可以实例化一个 BigInteger 对象,并自动调用相应的构造函数。BigInteger 类具有很多构造函数,但是最直接的一种方式是参数以字符串形式代表要处理的数字。即:

public BigInteger(String val);

其中,val 是十进制字符串。如,我们要将十进制2转换为 BigInteger 类型,则:

BigInteger value = new BigInteger("2");

需要注意的是参数2的双引号不能省略,因为参数是以字符串的形式存在的。

一旦创建了对象实例,就可以调用 BigInteger 类中的一些方法进行运算操作了,包括基本的数学运算和位运算以及一些取相反数、绝对值等操作。常用的 BigInteger 类的方法有:

  • public BigInteger add(BigInteger value):加法运算
  • public BigInteger subtract(BigInteger value):减法运算
  • public BigInteger multiply(BigInteger value):乘法运算
  • public BigInteger divide(BigInteger value):除法运算
  • public BigInteger remainder(BigInteger value):求余运算
  • public BigInteger[] divideAndRemainder(BigInteger value):以数组形式返回余数和商,结果数组中第一个值为商,第二个值为余数
  • public BigInteger pow(int exponent):进行取参数的 exponent 次方操作
  • public BigInteger negate():取相反数运算
  • public BigInteger shiftLeft(int n):将数字左移 n 位,如果 n 为负数,则作右移操作
  • public BigInteger shiftRight(int n):将数字右移 n 位,如果 n 为负数,则作左移操作
  • public BigInteger and(BigInteger value):与运算
  • public BigInteger or(BigInteger value):或运算
  • public int compareTo(BigInteger value):数字比较运算
  • public boolean equals(Object object):当参数 object 是 BigInteger 类型的数字并且数值相等时,返回 true
  • public BigInteger min(BigInteger value):返回较小的数值
  • public BigInteger max(BigInteger value):返回较大的数值

示例:创建 BigInteger 类的实例对象,并调用该对象的各种方法实现大整数的加减乘除等运算

import java.math.BigInteger;

public class BigIntegerDemo {

    public static void main(String[] args) {
        // 实例化一个大数字100
        BigInteger bigInteger = new BigInteger("100");
        System.out.println("加法运算:" + bigInteger.add(new BigInteger("100")));
        System.out.println("减法运算:" + bigInteger.subtract(new BigInteger("50")));
        System.out.println("乘法运算:" + bigInteger.multiply(new BigInteger("1000")));
        System.out.println("除法运算:" + bigInteger.divide(new BigInteger("20")));
        System.out.println("求余运算:" + bigInteger.remainder(new BigInteger("30")));
        System.out.println("次方运算:" + bigInteger.pow(4));
        System.out.println("求与运算:" + bigInteger.and(new BigInteger("200")));
        System.out.println("求或运算:" + bigInteger.or(new BigInteger("200")));
        System.out.println("比较运算:" + bigInteger.compareTo(new BigInteger("200")));
        System.out.println("判断是否相等:" + bigInteger.equals(new BigInteger("100")));
        System.out.println("min 运算:" + bigInteger.min(new BigInteger("50")));
        System.out.println("max 运算:" + bigInteger.max(new BigInteger("200")));
        System.out.println("取相反数:" + bigInteger.negate());
        System.out.println("取商运算:" + bigInteger.divideAndRemainder(new BigInteger("3"))[0]);
        System.out.println("取余运算:" + bigInteger.divideAndRemainder(new BigInteger("3"))[1]);
        System.out.println("左移运算:" + bigInteger.shiftLeft(2));
        System.out.println("右移运算:" + bigInteger.shiftRight(3));

    }

}

int java 最大取值 java int最大值和integer_int java 最大取值_02

BigDecimal

BigInteger 和 BigDecimal 都能实现大数字的运算,不同的是 BigDecimal 加入了小数的概念。一般的 float 型和 double 型数据只可以用来进行科学计算或工程计算,但由于在商业计算中要求的数字精度比较高,所以要用到 java.math.BigDecimal 类。BigDecimal 类支持任何精度的定点数,可以用它来精确计算货币值。在 BigDecimal 类中常用的两个构造方法为:

// 实例化时将双精度型转换为 BigDecimal 型
public BigDecimal(double value);

// 实例化时将字符串形式转换为 BigDecimal 型
public BigDecimal(String str);

BigDecimal 类型的数字可以用来做超大浮点数的运算,如加减乘除等,在所有的运算中,除法是最复杂的,因为在除不尽的情况下末位小数点的处理是需要特殊考虑的。常用的 BigDecimal 类的方法有:

  • public BigDecimal add(BigDecimal value):加法运算
  • public BigDecimal subtract(BigDecimal value):减法运算
  • public BigDecimal multiply(BigDecimal value):乘法运算
  • public BigDecimal divide(BigDecimal divisor, int scale, int roundingModel):除法运算,divisor 为除数,scale 为商的小数点的位数,roudingModel 对商的处理模式,如四舍五入

roundingModel 有多种模式,用于返回商末位小数点的处理,如下所示:

divide() 方法的多种处理模式

模式

说明

BigDecimal.ROUND_UP

商的最后一位如果大于0,则向前进位,正负数都是如此

BigDecimal.ROUND_DOWN

商的最后一位无论是什么数字,均省略,即舍去

BigDecimal.ROUND_CEILING

商如果是正数,按照 ROUND_UP 模式处理,如果是负数,则按照 ROUND_DOWN 模式处理。这两种模式的处理都会使近似值大于等于实际值

BigDecimal.ROUND_FLOOR

与 BigDecimal.ROUND_CEILING 模式相反,商如果是正数,按照 ROUND_DOWN 模式处理;商如果是负数,按照 ROUND_UP 模式处理。这两种模式的处理都会使近似值小于等于实际值

BigDecimal.ROUND_HALF_DOWN

对商进行四舍五入运算,如果商的最后一位小于等于5,则舍去

BigDecimal.ROUND_HALF_UP

对商进行四舍五入运算,如果商的最后一位小于5,则舍去

BigDecimal.ROUND_HALF_EVEN

如果商的倒数第二位为奇数,则按照 ROUND_HALF_UP 处理;如果为偶数,则按照 ROUND_HALF_DOWN 处理

示例:实现大数的加减乘除运算

import java.math.BigDecimal;

public class BigDecimalDemo {

    /**
     * 定义加法方法
     * @param value1
     * @param value2
     * @return
     */
    public BigDecimal add(double value1, double value2) {
        BigDecimal bigDecimal1 = new BigDecimal(Double.toString(value1));
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(value2));
        return bigDecimal1.add(bigDecimal2);
    }

    /**
     * 定义减法运算
     * @param value1
     * @param value2
     * @return
     */
    public BigDecimal sub(double value1, double value2) {
        BigDecimal bigDecimal1 = new BigDecimal(Double.toString(value1));
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(value2));
        return bigDecimal1.subtract(bigDecimal2);
    }

    /**
     * 定义乘法运算
     * @param value1
     * @param value2
     * @return
     */
    public BigDecimal mul(double value1, double value2) {
        BigDecimal bigDecimal1 = new BigDecimal(Double.toString(value1));
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(value2));
        return bigDecimal1.multiply(bigDecimal2);
    }

    /**
     * 定义除法运算
     * @param value1
     * @param value2
     * @param scale
     * @return
     */
    public BigDecimal div(double value1, double value2, int scale) {
        if (scale < 0) {
            System.out.println("scale 须大于等于 0 !");
        }
        BigDecimal bigDecimal1 = new BigDecimal(Double.toString(value1));
        BigDecimal bigDecimal2 = new BigDecimal(Double.toString(value2));
        return bigDecimal1.divide(bigDecimal2, scale, BigDecimal.ROUND_HALF_UP);
    }
    public static void main(String[] args) {

        BigDecimalDemo bigDecimal = new BigDecimalDemo();
        System.out.println("BigDecimal 加法运算:" + bigDecimal.add(-7.5, 8.9));
        System.out.println("BigDecimal 减法运算:" + bigDecimal.sub(-7.5, 8.9));
        System.out.println("BigDecimal 乘法运算:" + bigDecimal.mul(-7.5, 8.9));
        System.out.println("BigDecimal 除法运算:" + bigDecimal.div(-7.5, 8.9, 5));

    }

}

int java 最大取值 java int最大值和integer_System_03