栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java随笔-大数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java随笔-大数

大数

文章目录

大数

一:整形大数二:浮点型大数
若基本的整形和浮点型精度不够满足需求,那么可以使用java.math包中两个很有用的类:BigInteger和BigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现任意精度的整数运算,BigDecimal实现任意精度的浮点数运算。

一:整形大数

构造方法:

//构造方法一
BigInteger bigInteger = new BigInteger(String str);
//构造方法二
BigInteger bigInteger = BigInteger.valueOf(int x);

常用API:

//实现加法运算
BigInteger add(BigInteger other)
//减法运算
BigInteger subtract(BigInteger other)
//乘法运算
BigInteger multiply(BigInteger other)
//除法运算
BigInteger divide(BigInteger other)
//取模运算
BigInteger mod(BigInteger other)
//与另一个大数相比较,若相等返回0,小返回-1,大返回1
int compareTo(BigInteger other)'
//将基本的整形变成整形大数,返回值等于x的大数
static BigInteger valueOf(long x)

代码示例:

 public static void main(String[] args) {
        //整形大数
        //构造方法一
        BigInteger bigInteger = new BigInteger("1234567890234321");
        System.out.println(bigInteger);

        //构造方法二
        BigInteger bigInteger1 = BigInteger.valueOf(100);
        System.out.println(bigInteger1);

        //加:1234567890234321+100+100
        BigInteger sum = bigInteger.add(bigInteger1).add(BigInteger.valueOf(100));
        System.out.println(sum);

        //减1234567890234321-1234567890234321-100 = -100
        BigInteger sub = bigInteger.subtract(bigInteger).subtract(BigInteger.valueOf(100));
        System.out.println(sub);

        //乘
        BigInteger mut = bigInteger.multiply(BigInteger.valueOf(0)).add(bigInteger1);
        System.out.println(mut);

        //除
        BigInteger divide = bigInteger.divide(bigInteger);
        System.out.println(divide);

        //取模
        BigInteger mod = bigInteger.mod(BigInteger.valueOf(2));
        System.out.println(mod);

        //比较
        int i = bigInteger1.compareTo(BigInteger.valueOf(100));
        System.out.println(i);
        int i1 = bigInteger1.compareTo(BigInteger.valueOf(90));
        System.out.println(i1);
        int i2 = bigInteger1.compareTo(BigInteger.valueOf(110));
        System.out.println(i2);
    }

结果:

二:浮点型大数

构造方法:

    BigDecimal bigDecimal = new BigDecimal(int x);
    BigDecimal bigDecimal1 = new BigDecimal(double x);
    BigDecimal bigDecimal2 = new BigDecimal(String str);
    BigDecimal bigDecimal3 = new BigDecimal(BigInteger integer);

常用API:

BigDecimal add(BigDecimal other)
//减法运算
BigDecimal subtract(BigDecimal other)
//乘法运算
BigDecimal multiply(BigDecimal other)
//除法运算
//如果商是个寻仙循环的小数时,程序会抛出异常
BigDecimal divide(BigDecimal other)
//得到一个舍入的结果,如果是四舍五入,那么第二个参数是RoundingMode.HALF_UP
BigDecimal divide(BigDecimal other,RoundingMode mode)
//与另一个大数相比较,若相等返回0,小返回-1,大返回1
int compareTo(BigDecimal other)'
//将基本的整形变成整形大数,返回值等于x的大数
static BigDecimal valueOf(long x)

代码示例:

//构造方式
        BigDecimal bigDecimal = new BigDecimal(1000);
        BigDecimal bigDecimal1 = new BigDecimal(100.1);
        BigDecimal bigDecimal2 = new BigDecimal("10002");
        BigDecimal bigDecimal3 = new BigDecimal(bigInteger1);
        System.out.println(bigDecimal);
        System.out.println(bigDecimal1);
        System.out.println(bigDecimal2);
        System.out.println(bigDecimal3);
        //加
        BigDecimal add = bigDecimal.add(bigDecimal1);
        System.out.println(add);
        //减
        BigDecimal subtract = bigDecimal.subtract(bigDecimal1);
        System.out.println(subtract);
        //乘
        BigDecimal multiply = bigDecimal.multiply(BigDecimal.valueOf(2));
        System.out.println(multiply);

        //除
      //  BigDecimal divide1 = bigDecimal.divide(BigDecimal.valueOf(3));//结果是一个无限循环的小数,这种方法会报错
        BigDecimal divide2 = bigDecimal.divide(BigDecimal.valueOf(3), RoundingMode.UP);//对于无限循环的小数进行四舍五入
//        System.out.println(divide1);
        System.out.println(divide2);

        //比较
        int i3 = bigDecimal.compareTo(BigDecimal.valueOf(1000));
        int i4 = bigDecimal.compareTo(BigDecimal.valueOf(1));
        int i5 = bigDecimal.compareTo(BigDecimal.valueOf(1000000));
        System.out.println(i3);
        System.out.println(i4);
        System.out.println(i5);

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号