BigInteger和BigDecimal

目录

一、BigInteger

        1、在Java中,四种类型整数

        2、构造方法

        补

二、BigDecimal

        作用:1、用于小数的精确计算

                细节

                BigDecimal的使用


一、BigInteger

        1、在Java中,整数有四种类型:byte、short、int、long

                在底层占用字节数个数:byte个字节,short个字节,int个字节,long8个字节

        2、构造方法:

方法名 说明
public BigInteger(int num,Random rnd) 获取随机大整数范围:[((0~2)^num) - 1]
public BigInteger 获取指定大整数
public BigInter(String val,int radix) 获取指定进制的大整数
public static BigInteger valueOf(long val) 静态获取BigInteger的对象,内部有优化

                细节:对象一旦创建,内部记录的值不能发生改变

import java.math.BigInteger;
import java.util.Random;

public class a01biginteger {
    public static void main(String[] args) {
        Random r = new Random();
        //1、获取一个随机的大整数
        BigInteger bd1 = new BigInteger(4,r);
        System.out.println(bd1);
        //2、获取一个随机的大整数
        /*细节:字符串必须是整数,否则会报错*/
        BigInteger bd2 = new BigInteger("9999999990");
        System.out.println(bd2);
        //3、获取指定进制的大整数
        /*细节:
        * 1、字符串中的数字必须是整数
        * 2、字符串中的数字必须跟进制吻合,否则会报错*/
        BigInteger bd3 = new BigInteger("100",10);//100,表示:十进制的100为100
        System.out.println(bd3);
        BigInteger bd4 = new BigInteger("100",2);//4,表示:二进制的100为4
        System.out.println(bd4);
        //4、静态方法获取BigInteger的对象,内部有优化
        /*细节:
        * 1、能表示的范围较小,只能在long的取值范围内,如果超出long的范围就不行了
        * 2、在内部对常用的数字:-16~16进行了优化。提前把-16~16先创建好BigInteger对象
        * 如果多次获取不会创建新的*/
        BigInteger bd5 = BigInteger.valueOf(16);
        BigInteger bd6 = BigInteger.valueOf(16);
        System.out.println(bd5 == bd6);//true
        BigInteger bd7 = BigInteger.valueOf(17);
        BigInteger bd8 = BigInteger.valueOf(17);
        System.out.println(bd7 == bd8);//false
        //5、对象一旦创建,内部的数据不能发生改变
        BigInteger bd9 = BigInteger.valueOf(1);
        BigInteger bd10 = BigInteger.valueOf(2);
        BigInteger result = bd9.add(bd10);
        System.out.println(result);//3
        System.out.println(result == bd9);//false
        System.out.println(result == bd10);//false
    }
}

输出结果:

BigInteger和BigDecimal_第1张图片

        补:1、如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取。

                2、如果BigInteger表示的超出long范围,可以用构造方法获取

方法名 说明
public BigInteger add(BigInteger val) 加法
public BigInteger substract(BigInter val) 减法
public BigInteger multiply(BigInter val) 乘法
public BigInteger divide(BigInter val) 除法,获取商
public BigInteger [ ] divideAndRemainder(BigInter val) 除法,获取商和余数
public boolean equals(Object x) 比较是否相同
public BigInteger pow(int exponent) 次幂
public BigInteger max/min(BigInter val) 返回较大值/较小值
public int intValue(BigInter val) 转为int类型整数,超出范围数据有误
import java.math.BigInteger;

public class a01biginteger01 {
    public static void main(String[] args) {
        //1、创建对象
        BigInteger bd1 = BigInteger.valueOf(10);
        BigInteger bd2 = BigInteger.valueOf(2);
        //加法
        BigInteger bd3 = bd1.add(bd2);//12
        //减法
        BigInteger bd4 = bd1.subtract(bd2);//8
        //乘法
        BigInteger bd5 = bd1.multiply(bd2);//20
        //除法,获取商
        BigInteger bd6 = bd1.divide(bd2);//5
        //除法获取商和余数,返回数组
        BigInteger[] bd7 = bd1.divideAndRemainder(bd2);
        System.out.println(bd7.length);//2
        System.out.println(bd7[0]);//5
        System.out.println(bd7[1]);//0
        //比较是否相等
        boolean bd8 = bd1.equals(bd2);//false
        //次幂
        BigInteger bd9 = bd1.pow(2);//100
        //返回较大值和较小值
        BigInteger bd10 = bd1.max(bd2);//10
        BigInteger bd11 = bd1.min(bd2);//2
        //转为int类型的整数
        BigInteger bd12 = new BigInteger("1888974");//1888974
        int i = bd12.intValue();
        System.out.println(bd3);
        System.out.println(bd4);
        System.out.println(bd5);
        System.out.println(bd6);
        System.out.println(bd8);
        System.out.println(bd9);
        System.out.println(bd10);
        System.out.println(bd11);
        System.out.println(bd12);
        System.out.println(i);
    }
}

输出结果:

BigInteger和BigDecimal_第2张图片

二、BigDecimal

        作用:1、用于小数的精确计算

                   2、用来表示很大的小数

方法名 说明
public BigDecimal(double val)

通过传递double类型的小数来创建对象。

细节:这种方法有可能是不精确的,所以不建议使用

public BigDecimal(String val) 通过字符串来创建对象
public static BigDecimal( valueOf(double val)) 通过静态方法获取对象

                细节:

                        1、如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法

                        2、如果要表示的数字比较大,超出了double的取值范围,建议使用构造方法

                        3、如果我们传递的是0~10之间的整数,包含0,包含10,那么方法回返回已经创建好的对象,不会重新new。

                BigDecimal的使用

方法名 说明
public static BigDecimal valueOf(double val) 获取对象
public BigDecimal add(BigDecimal val) 加法
public BigDecimal substract(BigDecimal val) 减法
public BigDecimal multiply(BigDecimal val) 乘法
public BigDecimal divide(BigDecimal val) 除法
public BigDecimal divide(BigDecimal val,精确几位,舍入模式) 除法(常见的有四舍五入:RoundingMode.HALF.UP)
import java.math.BigDecimal;
import java.math.BigInteger;

public class a02biddecimal {
    public static void main(String[] args) {
        BigDecimal bd1 = BigDecimal.valueOf(10.0);
        BigDecimal bd2 = BigDecimal.valueOf(3.0);
        //加法
        BigDecimal bd3 = bd1.add(bd2);
        System.out.println(bd3);//13.0
        BigDecimal bd4= bd1.subtract(bd2);
        System.out.println(bd4);//7.0
        BigDecimal bd5 = bd1.multiply(bd2);
        System.out.println(bd5);//30.00
        BigDecimal bd6 = bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP);
        System.out.println(bd6);//四舍五入保留两位小数。3.33
    }
}

输出结果: 

BigInteger和BigDecimal_第3张图片

你可能感兴趣的:(开发语言,java)