Java中BigDecimal和BigInteger用法知识点补充

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 Java中BigDecimal和BigInteger用法知识点补充,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

今天开发过程中遇到了数据库字段的属性为BigDecimal,对这个属性有点陌生,查看了下JavaAPI和阿里Java编码规范中和Mysql建表约束中明确规范,写几个BigDecimal类型的方法熟悉下用法,同时还遇到BigInteger的用法,一起特此记录下!

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

BigDecimal

java.lang.Object
java.lang.Number
java.math.BigDecimal

BigDecimal可以表示一个任意大小且精度完全准确的浮点数

public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("-15896.789");
        // 返回一个 BigDecimal ,其值为此 BigDecimal的绝对值
        System.out.println("bigDecimal = " + bigDecimal);
        // 转换为short:shortValue()
        System.out.println("bigDecimal.shortValue() = " + bigDecimal.shortValue());
        // 转换为int:intValue()
        System.out.println("bigDecimal.intValue() = " + bigDecimal.intValue());
        // 转换为byte:byteValue()
        System.out.println("bigDecimal.byteValue() = " + bigDecimal.byteValue());
        // 转换为float:floatValue()
        System.out.println("bigDecimal.floatValue() = " + bigDecimal.floatValue());
        // 转换为double:doubleValue()
        System.out.println("bigDecimal.doubleValue() = " + bigDecimal.doubleValue());
        // 转换为long:longValue()
        System.out.println("bigDecimal.longValue() = " + bigDecimal.longValue());
        System.out.println("bigDecimal.max(bigDecimal) = " + bigDecimal.max(bigDecimal));
        // 返回此BigDecimal对象的精度
        System.out.println("bigDecimal.scale() = " + bigDecimal.scale());
        // 转为字符串
        System.out.println("bigDecimal.toString() = " + bigDecimal.toString());
        // 返回一个 BigDecimal ,相当于这个小数点,向左移动了 n个地方
        System.out.println("bigDecimal.movePointLeft(2) = " + bigDecimal.movePointLeft(2));
        // 返回一个 BigDecimal ,相当于这个小数点,向右移动了 n个地方
        System.out.println("bigDecimal.movePointRight(2) = " + bigDecimal.movePointRight(2));
        // 返回 BigDecimal ,值为 bigDecimal的平方
        System.out.println("bigDecimal.multiply(bigDecimal) = " + bigDecimal.multiply(bigDecimal));
        // 返回 BigDecimal ,值为 bigDecimal+bigDecimal
        System.out.println("bigDecimal.add(bigDecimal) = " + bigDecimal.add(bigDecimal));
    }

控制台输出

bigDecimal = -15896.789
bigDecimal.shortValue() = -15896
bigDecimal.intValue() = -15896
bigDecimal.byteValue() = -24
bigDecimal.floatValue() = -15896.789
bigDecimal.doubleValue() = -15896.789
bigDecimal.longValue() = -15896
bigDecimal.max(bigDecimal) = -15896.789
bigDecimal.scale() = 3
bigDecimal.toString() = -15896.789
bigDecimal.movePointLeft(2) = -158.96789
bigDecimal.movePointRight(2) = -1589678.9
bigDecimal.multiply(bigDecimal) = 252707900.510521
bigDecimal.add(bigDecimal) = -31793.578

BigInteger

java.lang.Object
java.lang.Number
java.math.BigInteger

BigInteger提供了所有Java的原始整数运算符和java.lang.Math中所有相关方法的类比。 此外,BigInteger还提供了模数运算,GCD计算,原始测试,初级生成,位操作以及其他一些其他操作的操作。

java.math.BigInteger就是用来表示任意大小的整数。BigInteger内部用一个int[]数组来模拟一个非常大的整数

BigInteger的构造方法

在这里插入图片描述

如果BigInteger表示的范围超过了基本类型的范围,在转换时如果超出范围,将直接抛出ArithmeticException异常

public static void main(String[] args) {
        BigInteger bigInteger = new BigInteger("1234566666");
        BigInteger bigInteger1 = new BigInteger("123467896666");
        BigInteger bigInteger2 = new BigInteger("789412354666");
        BigInteger bigInteger3 = new BigInteger("12966600000");
        System.out.println("bigInteger.pow(5) = " + bigInteger.pow(5));
        System.out.println("bigInteger1.add(bigInteger2) = " + bigInteger1.add(bigInteger2));
        // 转换为short:shortValue()
        System.out.println("bigInteger3.shortValue() = " + bigInteger3.shortValue());
        // 转换为int:intValue()
        System.out.println("bigInteger3.intValue() = " + bigInteger3.intValue());
        // 转换为float:floatValue()
        System.out.println("bigInteger3.floatValue() = " + bigInteger3.floatValue());
        // 转换为double:doubleValue()
        System.out.println("bigInteger3.doubleValue() = " + bigInteger3.doubleValue());
        // 转换为byte:byteValue()
        System.out.println("bigInteger3.byteValue() = " + bigInteger3.byteValue());
        // 转换为long:longValue()
        System.out.println("bigInteger3.longValue() = " + bigInteger3.longValue());
        // 如果`BigInteger`表示的范围超过了基本类型的范围,
        // 在转换时如果超出范围,将直接抛出`ArithmeticException`异常
        System.out.println("(bigInteger3.multiply(bigInteger3).longValueExact()) = " + (bigInteger3.multiply(bigInteger3).longValueExact()));
    }

控制台输出

bigInteger.pow(5) = 2867957643217673649618338593780775741137020576
bigInteger1.add(bigInteger2) = 912880251332
bigInteger3.shortValue() = -25280
bigInteger3.intValue() = 81698112
bigInteger3.floatValue() = 1.29665997E10
bigInteger3.doubleValue() = 1.29666E10
bigInteger3.byteValue() = 64
bigInteger3.longValue() = 12966600000
Exception in thread "main" java.lang.ArithmeticException: BigInteger out of long range

小结

  • BigDecimal用于表示精确的小数

  • 比较BigDecimal的值是否相等,必须使用compareTo()而不能使用equals()

  • BigInteger表示任意大小的整数

  • BigInteger转换为基本类型可使用longValueExact()等方法保证结果准确

参考资料
BigInteger

BigDecimal

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/140696.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!