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

今天开发过程中遇到了数据库字段的属性为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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值