首页 > 其他分享 >API(BigInteger,BigDecimal)

API(BigInteger,BigDecimal)

时间:2023-01-27 14:00:11浏览次数:35  
标签:BigInteger BigDecimal System b5 API println out

  1. BigInteger的对象一旦创建,内部的值不会发生改变,如果参与运算,则会产生一个新的BigIneger对象来接收
  2. 如果BigInteger的值没有超过long的范围,可以用静态方法获取
    //静态方法获取BigInteger对象,内部有优化
    //细节:1:能表示的范围比较小,只能在long范围内
    //2;在内部对常用的数字进行了优化:-16~16,提前把-16~正16的BigInteger的对象创建好,如果多次获取不会重新创建
    System.out.println(BigInteger.valueOf(100));
    BigInteger bigInteger = BigInteger.valueOf(16);
    BigInteger bigInteger1 = BigInteger.valueOf(16);
    System.out.println(bigInteger==bigInteger1);//true
  3. 如果BigInteger的取值范围超过long的取值范围,可以用构造方法获取
    //获取一个随即大的整数(前面一个参数为2的几次方,后面的为随机数)
    BigInteger b1= new BigInteger(4,new Random());
    System.out.println(b1);
    //获取一个指定大的整数
    //细节:字符串中必须为整数
    BigInteger b2= new BigInteger("23");
    System.out.println(b2);
    //获取指定进制的大整数(前面的为阿拉伯整数,后面的参数为指定进制)
    BigInteger b3 = new BigInteger("123",8);
    System.out.println(b3);
  4. BigInteger中常用的方法
    //加法
    BigInteger b5= BigInteger.valueOf(10);
    BigInteger b6= BigInteger.valueOf(10);
    BigInteger add = b5.add(b6);
    System.out.println(add);
    //减法
    BigInteger subtract = b5.subtract(b6);
    System.out.println(subtract);
    //乘法
    BigInteger multiply = b5.multiply(b6);
    System.out.println(multiply);
    //除法,返回商
    BigInteger divide = b5.divide(b6);
    System.out.println(divide);
    //除法,返回商和余数,0索引代表商,1索引代表长度,数组长度为2
    BigInteger[] bigIntegers = b5.divideAndRemainder(b6);
    System.out.println(bigIntegers);
    //比较是否相同
    boolean equals = b5.equals(b6);
    System.out.println(equals);
    //次幂
    BigInteger pow = b5.pow(2);
    System.out.println(pow);
    //返回较大值,较小值
    BigInteger max = b5.max(b6);
    BigInteger min = b5.min(b6);
    //转化为int类型,超出范围则数据有误
    int i = b5.intValue();
    System.out.println(i);

  5. BigDecimal的对象一旦创建,内部的值不会发生改变,如果参与运算,则会产生一个新的BigDecimal的对象来接收
    //通过传递double类型的小数来创建对象
    //细节:可能会不精确,不建议使用
    BigDecimal B = new BigDecimal(0.1);
    System.out.println(B);
    //通过传递字符串类型的小数创建对象,精度不会丢失
    BigDecimal bigDecimal=new BigDecimal("0.1");
    System.out.println(bigDecimal);
  6. BigDecimal静态方法
    //通过静态方法获取对象
    BigDecimal bigDecimal1 = BigDecimal.valueOf(10);
    System.out.println(bigDecimal1);

标签:BigInteger,BigDecimal,System,b5,API,println,out
From: https://www.cnblogs.com/lyjzht/p/17068867.html

相关文章