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

27.Java之BigInteger和BigDecimal(加、减、乘、除运算)

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

27.Java之BigInteger和BigDecimal(加、减、乘、除运算)

27.1 BigInteger 和 BigDecima 介绍
  1. BigInteger 适合保存比较大的整型
  2. BigDecimal 适合保存精度更高的浮点型(小数)
27.2 BigInteger 应用
BigInteger bigInteger1 = new BigInteger("2315456456456561515121212121115");
BigInteger bigInteger2 = new BigInteger("1524154515415641564564564161515615615151");
BigInteger add = bigInteger1.add(bigInteger2);
BigInteger subtract = bigInteger1.subtract(bigInteger2);
BigInteger multiply = bigInteger1.multiply(bigInteger2);
BigInteger divide = bigInteger2.divide(bigInteger1);

除完是保留整型,没有四舍五入

27.3 BigDecimal 应用
BigDecimal bigDecimal1 = new BigDecimal("23.154515415456456158884415154845156464845");
BigDecimal bigDecimal2 = new BigDecimal("12.4545254");
BigDecimal add = bigDecimal1.add(bigDecimal2);
BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
BigDecimal divide = bigDecimal1.divide(bigDecimal2, MathContext.DECIMAL32);

可能会抛出异常-ArithmeticException
所以在调用该方法时,指定精度即可

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

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

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