JAVA基础知识——大数值

大数值:

        java.math 类中有两个常用的类:BigInteger,BigDecimal

        BigInteger类实现了任意精度的整数运算,BigDecimal类实现了任意精度的浮点数运算

        使用静态的valueOf方法可以将普通的数值转换为大数值:BigInteger a = BigInteger.vslueOf(100);但不能使用+和*,而需要使用大数值中的add(加)、subtract(减)、multiply(乘)、divide(除)、mod(取余)方法    (将BigInteger该成BigDecimal,下面的方法就都可以使用了(用来计算浮点数运算))

        BigInteger c = a.add(b);   //c= a+b

          BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));  //d=c*(b+2)       

        BigInteger a = BigInteger.valueOf(4); //大数值
        BigInteger b = BigInteger.valueOf(5);


        BigInteger c = a.add(b); //加
        System.out.println(c); //9


        BigInteger d = a.multiply(b.add(a));//乘
        System.out.println(d); //36


        BigInteger q = b.subtract(a); //减
        System.out.println(q); //1


        BigInteger e = a.multiply(BigInteger.valueOf(2));
        System.out.println(e); //8


        BigInteger f = a.subtract(b);//减
        System.out.println(f); //-1


        BigInteger g = a.mod(b); //取余
        System.out.println(g); //4 


        int w = a.compareTo(b); //如果a=b,返回0  a<b,返回负数   a>b,返回整数
        System.out.println(w);  //-1

        

        BigInteger bi ;    //static BigInteger valueOf(long x)返回值等于x的大整数
        Long l = new Long(123456789L);
        bi = BigInteger.valueOf(l);
        String str = "BigInteger value of Long "+ l + "is" + bi;
        System.out.println(str);

      

转载于:https://www.cnblogs.com/shuai1154644882/p/6546493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值