目录
- 大整数操作类:BigInteger 概述
- 大小数操作类:BigDecimal
字符串表示大数字, "123456789123456789123456789123456789123456789"
(1)java.math.BigInteger 大整数操作类
(2)java.math.BigDecimal 大小数操作类
这两个类和 Java 中的表示数字的基本数据类型的包装类(Byte、Short、Integer、Long、 Double、Float)都是继承自 Number,即 Number 类的子类。
大整数操作类:BigInteger 概述
是一个用于表示任意精度整数的类。它可以处理仅在
或
范围内的整数运算以及超出这些范围的整数。
主要特点
(1)精度:不受
和
限制,可以处理非常大的整数。
(2)不支持小数:只能表示整数。
(3)所有操作(加法、减法、乘法、除法等)都通过方法实现,不能直接使用运算符。
使用场景
(1)计算大数的结果(如科学计算、密码学等)。
(2)需要精确的整数计算(如金融、统计分析等)。
(3)需要处理大范围数值的算法(如大数运算、阶乘计算等)。
大整数操作类:BigInteger 可以操作无限大的整型数据。
构造器
对于 BigInteger 类有很多个构造器,但是最常用的就是有一个 String 类型参数的构造器:
- public BigInteger(String val) {
- this(val, 10);
- }
复制代码
常用方法
(1)BigInteger add(BigInteger val)
加法操作;
(2)BigInteger subtract(BigInteger val)
减法操作;
(3)BigInteger multiply(BigInteger val)
乘法操作;
(4)BigInteger divide(BigInteger val)
除法操作(不保留小数部分);
(5)BigInteger[] divideAndRemainder(BigInteger val)
除法操作,返回值是一个长度为 2 的数组,数组中的第一个元素表示商,数组中第二个元素表示余 数。
示例:
- import java.math.BigInteger;
- public class Test {
- public static void main(String[] args) {
- BigInteger bigInteger1 = new BigInteger("123456789123456789123456789123456789123456789");
- BigInteger bigInteger2 = new BigInteger("123456789123456789123456789123456789123456789");
- System.out.println(bigInteger1);
- System.out.println(bigInteger2);
- // 加法
- BigInteger plus = bigInteger1.add(bigInteger2);
- // 减法
- BigInteger subtract = bigInteger1.subtract(bigInteger2);
- // 乘法
- BigInteger multiply = bigInteger1.multiply(bigInteger2);
- // 除法
- BigInteger divide = bigInteger1.divide(bigInteger2);
- System.out.println(plus);
- System.out.println(subtract);
- System.out.println(multiply);
- System.out.println(divide);
- }
- }
复制代码
打印结果:
大小数操作类:BigDecimal
概述
是一个用于表示任意精度的浮点数的类。它具有控制小数位数和舍入策略的功能。
主要特点
(1)精度高,不会受到
和
的限制,适用于需要精确表示小数点数据的场景。
(2)支持控制小数点后的位数及舍入方式。
(3)通过方法实现计算,不能直接使用运算符。
使用场景
(1)金融计算:涉及到货币或者利息计算时,确保不会因为浮点数运算导致的精度损失。
(2)科学计算:需要更高精度的小数计算。
(3)需要高精度结果的任何应用(如价格计算、统计数据处理中)。
构造器
BigDecimal(String val) 创建一个表示字符串参数 val 的 BigDecimal 类型的大小数对象。
常用方法
(1)BigDecimal add(BigDecimal augend) 加法操作;
(2)BigDecimal subtract(BigDecimal subtrahend) 减法操作;
(3)BigDecimal multiply(BigDecimal multiplicand) 乘法操作;
(4)BigDecimal divide(BigDecimal divisor) 除法操作,如果除不尽,则会报 java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result;
(5)BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) 除法操作,可以设置结果保留的小数的位数以及进位模式,scale 参数表示保留的小数位数, roundingMode 参数表示进位模式。BigDecimal 类中定义多个静态常量表示进位模式, ROUND_HALF_UP 这个静态常量就表示四舍五入。
示例:
- import java.math.BigDecimal;
- public class Test {
- public static void main(String[] args) {
- BigDecimal bigDecimal1 = new BigDecimal("123456789123456789123456789123456789123456789.123456789123456789123456789123456789123456789");
- BigDecimal bigDecimal2 = new BigDecimal("123456789123456789123456789123456789123456789.123456789123456789123456789123456789123456789");
- System.out.println(bigDecimal1);
- System.out.println(bigDecimal2);
- // 加法
- BigDecimal plus = bigDecimal1.add(bigDecimal2);
- // 减法
- BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
- // 乘法
- BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
- // 除法
- BigDecimal divide = bigDecimal1.divide(bigDecimal2);
- System.out.println(plus);
- System.out.println(subtract);
- // 0E-45 是一个科学计数法的表示方式,表示的是非常靠近于零的数。此处的E-45是表示小数点位置,虽然数字为零,但由于内部存储的计算精度导致了这样的表示。
- // 修改输出格式以普通数字显示
- System.out.println(subtract.stripTrailingZeros().toPlainString());
- System.out.println(multiply);
- System.out.println(divide);
- }
- }
复制代码
打印结果:
定义声明一个方法,可以对 double 类型的小数进行四舍五入的操作
示例:
- import java.math.BigDecimal;
- public class Test {
- public static void main(String[] args) {
- double i = 3.1415926;
- System.out.println(round(i,2));
- }
- /**
- *
- * @param num 传入的 double 类型的数据
- * @param scale 保留位数
- * @return
- */
- public static double round(double num,int scale) {
- BigDecimal decimal = new BigDecimal(num);
- //进行除法操作,除以 1 来控制结果保留多少位小数
- BigDecimal result =
- decimal.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);
- //把 BigDecimal 类型的结果转换为 double 类型返回
- return result.doubleValue();
- }
- }
复制代码
打印结果:
到此这篇关于JAVA 中的大数字操作类的文章就介绍到这了,更多相关java大数字操作类内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!