• JAVA BigInter


    使用long等基本数据类型,标识的数据范围是有限的,JAVA中使用BigInter模拟大整数运算

    BigInter在数学就是表示一个比较大的整数,在JAVA内部用一个int[]数组来模拟,没什么特别;

    使用上,和基本类型有一些要注意:

    BigInteger i1 = new BigInteger("1234567890");
    BigInteger i2 = new BigInteger("12345678901234567890");
    BigInteger sum = i1.add(i2); // 12345678902469135780
    

    long型整数运算比,BigInteger不会有范围限制,超出float表示的范围时,输出 Infinity

    四则运算需要使用实例化方法

    BigInteger i = new BigInteger("123456789000");
    System.out.println(i.longValue()); // 123456789000
    System.out.println(i.multiply(i).longValueExact()); // java.lang.ArithmeticException: BigInteger out of long range
    

    longValueExact 方法可以在运算超出范围时抛出异常;此外还有intValueExact

    转换为基本类型:

    • 转换为bytebyteValue()
    • 转换为shortshortValue()
    • 转换为intintValue()
    • 转换为longlongValue()
    • 转换为floatfloatValue()
    • 转换为doubledoubleValue()
  • 相关阅读:
    OCP-1Z0-053-200题-36题-615
    Android换行符变成方框的解决方法
    OCP-1Z0-053-200题-35题-614
    FusionCharts 3D帕累托图
    FusionCharts 3D帕累托图报错
    FusionCharts 2D帕累托图
    OCP-1Z0-053-200题-33题-612
    OCP-1Z0-053-V13.02-612题
    OCP-1Z0-053-200题-32题-611
    OCP-1Z0-053-V13.02-611题
  • 原文地址:https://www.cnblogs.com/debug-the-heart/p/13718824.html
Copyright © 2020-2023  润新知