大数值——BigInteger类与BigDecimal类的概述

大数值——BigInteger类与BigDecimal类的概述

BigInteger类

概述

可以让超过Integer(主要用来与String进行转换)范围内的数据进行加减乘除的运算。

测试构造方法: BigInteger(String val)

public class BigIntegerDemo {
    public static void main(String[] args) {
        // 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。
        // Integer i = new Integer(100);
        // System.out.println(i);
        // // System.out.println(Integer.MAX_VALUE);
        // Integer ii = new Integer("2147483647");
        // System.out.println(ii);
        // // NumberFormatException
        // Integer iii = new Integer("2147483648");
        // System.out.println(iii);

        // 通过大整数来创建对象
        BigInteger bi = new BigInteger("2147483648");
        System.out.println("bi:" + bi);
    }
}

测试主要方法:

public BigInteger add(BigInteger val):加

public BigInteger subtract(BigInteger val):减

public BigInteger multiply(BigInteger val):乘

public BigInteger divide(BigInteger val):除

public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

public class BigIntegerDemo {
    public static void main(String[] args) {
        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("50");

        // public BigInteger add(BigInteger val):加
        System.out.println("add:" + bi1.add(bi2));
        // public BigInteger subtract(BigInteger val):加
        System.out.println("subtract:" + bi1.subtract(bi2));
        // public BigInteger multiply(BigInteger val):加
        System.out.println("multiply:" + bi1.multiply(bi2));
        // public BigInteger divide(BigInteger val):加
        System.out.println("divide:" + bi1.divide(bi2));

        // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
        BigInteger[] bis = bi1.divideAndRemainder(bi2);
        System.out.println("商:" + bis[0]);
        System.out.println("余数:" + bis[1]);
    }
}

BigDecimal类

概述

float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。

由于在运算的时候,float类型和double很容易丢失精度。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal类

BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

主要用法:

构造方法:

public BigDecimal(String val)

主要方法:

public BigDecimal add(BigDecimal augend)

public BigDecimal subtract(BigDecimal subtrahend)

public BigDecimal multiply(BigDecimal multiplicand)

public BigDecimal divide(BigDecimal divisor)

public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取

public class BigDecimalDemo {
    public static void main(String[] args) {
        // System.out.println(0.09 + 0.01);
        // System.out.println(1.0 - 0.32);
        // System.out.println(1.015 * 100);
        // System.out.println(1.301 / 100);

        BigDecimal bd1 = new BigDecimal("0.09");
        BigDecimal bd2 = new BigDecimal("0.01");
        System.out.println("add:" + bd1.add(bd2));
        System.out.println("-------------------");

        BigDecimal bd3 = new BigDecimal("1.0");
        BigDecimal bd4 = new BigDecimal("0.32");
        System.out.println("subtract:" + bd3.subtract(bd4));
        System.out.println("-------------------");

        BigDecimal bd5 = new BigDecimal("1.015");
        BigDecimal bd6 = new BigDecimal("100");
        System.out.println("multiply:" + bd5.multiply(bd6));
        System.out.println("-------------------");

        BigDecimal bd7 = new BigDecimal("1.301");
        BigDecimal bd8 = new BigDecimal("100");
        System.out.println("divide:" + bd7.divide(bd8));
        System.out.println("divide:"
                + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
        System.out.println("divide:"
                + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: BigInteger转换为BigDecimal可以通过BigDecimal的构造函数来实现,将BigInteger作为参数传递给BigDecimal构造函数即可。 例如: BigInteger bigInteger = new BigInteger("12345678901234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); 这样就将BigInteger型的bigInteger转换为了BigDecimal型的bigDecimal。 ### 回答2: BigIntegerBigDecimal都是Java中处理大数的BigInteger表示不可变的任意精度整数,而BigDecimal表示不可变的任意精度十进制数。 要将BigInteger转换为BigDecimal,可以使用BigDecimal的构造方法来完成转换。BigDecimal的构造方法接受一个BigInteger型的参数,可以直接将BigInteger转换为相应的BigDecimal对象。 以下是将BigInteger对象转换为BigDecimal对象的示例代码: ```java BigInteger bigInteger = new BigInteger("1234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); ``` 上述代码中,首先创建了一个BigInteger对象bigInteger,其值为"1234567890"。然后使用BigDecimal的构造方法将bigInteger转换为相应的BigDecimal对象bigDecimal。 通过以上代码,我们成功将BigInteger对象转换为BigDecimal对象。这样就可以继续使用BigDecimal提供的各种方法进行高精度的数值计算。 需要注意的是,由于BigDecimal表示的是十进制数,而BigInteger表示的是整数,所以在转换时可能会导致精度的损失或受到舍入模式的影响。因此,在进行大数计算时,需要根据具体的需求选择合适的转换方式和精度控制。 ### 回答3: 在Java中,BigIntegerBigDecimal是数字处理中非常常用的两个BigInteger用于处理任意长度的整数,而BigDecimal用于处理任意精度的小数。 要将BigInteger转换为BigDecimal,可以使用BigDecimal的构造函数。BigDecimal有一个可以接受BigInteger值的构造函数,这样可以将BigInteger的值转换为BigDecimal。 以下是一个简单的示例代码,演示如何将BigInteger转换为BigDecimal: ```java import java.math.BigDecimal; import java.math.BigInteger; public class BigIntegerToBigDecimal { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("1234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); System.out.println("BigInteger: " + bigInteger); System.out.println("BigDecimal: " + bigDecimal); } } ``` 在上述示例中,我们首先创建了一个BigInteger对象(bigInteger),并将其初始化为1234567890。然后,我们使用BigDecimal的构造函数将bigInteger转换为BigDecimal(bigDecimal)。最后,我们将bigIntegerbigDecimal的值输出到控制台上。 输出结果: BigInteger: 1234567890 BigDecimal: 1234567890 通过这种方式,我们可以将BigInteger转换为BigDecimal,用于处理高精度的小数计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值