首页 > 其他 > 详细

BigInteger类和BigDecimal类

时间:2019-07-01 16:03:01      阅读:81      评论:0      收藏:0      [点我收藏+]

BigInteger

  java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.

  BigInteger类的构造方法:

 BigInteger b = new BigInteger(str);

  构造方法中,采用字符串的形式给出整数

四则运算代码:

public static void main(String[] args) {

     //大数据封装为BigInteger对象

          BigInteger big1 = new BigInteger("12345678909876543210");

          BigInteger big2 = new BigInteger("98765432101234567890");

          //add实现加法运算

          BigInteger bigAdd = big1.add(big2);

          //subtract实现减法运算

          BigInteger bigSub = big1.subtract(big2);

          //multiply实现乘法运算

          BigInteger bigMul = big1.multiply(big2);

          //divide实现除法运算

          BigInteger bigDiv = big2.divide(big1);

}

 

BigInteger类和BigDecimal类

原文:https://www.cnblogs.com/qq1312583369/p/11114286.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!