首页IT科技财务精度2(财务精度:BigInteger 与 BigDecimal)

财务精度2(财务精度:BigInteger 与 BigDecimal)

时间2025-06-20 05:10:10分类IT科技浏览4066
导读:财务精度:BigInteger 与 BigDecimal 每博一文案...

财务精度:BigInteger 与 BigDecimal

每博一文案

师父说: 人这一辈子            ,真地好难           。 有些人                 ,好着好着      ,忽然就变陌生了      ,有些手                 ,牵着牵着           ,瞬间就放开了      ,有些路                  ,走着走着           ,就失去了方向了                  。 懵懵懂懂,一眨眼                  ,我们就长大了                 ,爱过的人,一转身            ,                 ,青春就溜走了      。以为有来日方长的      ,最后只剩人走茶凉           。 以为能护你周全的            ,把你留给大风大浪                 。时光会老                 ,爱会退潮      ,猜不透的      ,是人心                 ,回不去           ,是从前      。 从早晨到天黑      ,忙忙碌碌就是一天                  ,从年初道年尾           ,辛辛苦苦就是一年      。 为了家人,再苦也要咬牙奋斗                  ,为了生活                 ,再累也要微笑面对                 。 道不尽的,是付出            ,丢不掉的                 ,是责任            。人累了就休息      ,没有铁打的身体            ,心累了就放下                 ,不是你的留不住      。 一生很短      ,不要追得太多      ,心也有限                 ,不必装太满                 。家不求奢华           ,只愿温馨和睦      , 爱不求浪漫                  ,只愿一生相伴            。凡事看开了           ,烦恼就少了,人心看淡了                  ,受伤就少了                 , 感情,大不了就是一聚一散            ,生活                 ,大不了就是一起一落      ,相爱            ,有苦有甜才叫日子                 ,心情      ,有起有落才叫人生。 愿你想开了就不必困惑      ,参透了就不用执着                 。 —————— 一禅心灵庙语

1. BigInteger

Integer 类作为 int 的包装类                 ,能存储的最大整型值为 2^31-1           ,Long 类也是有限的      ,最大为 2^63-1                  。如果要表示再大的整数                  ,不管是基本数据类型还是他们的包装类都无能为力           ,更不用说进行运算了。

java.math 包的 BigInteger 可以表示不可变的任意精度的整数           。BigInteger 提供所有 java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法                  。另外                  ,BigInteger 还提供以下运算:模算术                 ,GCD 计算,质数测试            ,素数生成                 ,位操作以及一些其他操作      。

一般使用 public BigInteger(String val) 构造器创建 Bigteger 对象

import java.math.BigInteger; public class BigIntegerTest { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("9999999999"); System.out.println(bigInteger); } }

1.2 BigInteger 常用的方法

1.2.1 BigInteger 的             ”+“ add(), "-"subtract      ,"*" multiply            ,"/" divide

BigInteger 是引用数据类型                 ,不是基本数据类型      ,是不可以直接使用 "+.-.*./" 这些算术运算符的      ,而是通过调用其对应的对象方法才行           。

+ 加 public BigInteger add(BigInteger val) - 减 public BigInteger subtract(BigInteger val) * 乘 public BigInteger multiply(BigInteger val) / 除public BigInteger divide(BigInteger val) 注意所传的参数 BigInteger 类型的才行的                 ,以及是加减乘除后           ,返回一个新的 BigInteger 对象不是      ,在原本的基础上修改的                  。 BigInteger 继承了 Number 类                  ,其 Integer 也是继承了该 Number 类      。

举例:

import java.math.BigInteger; public class BigIntegerTest { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("9999999999"); BigInteger bigInteger2 = new BigInteger("1"); BigInteger add = bigInteger.add(bigInteger2); // + System.out.println(add); BigInteger subtract = bigInteger.subtract(bigInteger2); // - System.out.println(subtract); BigInteger multiply = bigInteger.multiply(bigInteger2); // * System.out.println(multiply); BigInteger divide = bigInteger.divide(bigInteger2); // / System.out.println(divide); } } 1.2.2 绝对值:abs()            ,取模:remainder() ,次方:pow() public BigInteger abs(); // 返回其绝对值      。 public BigInteger remainder(BigInteger val); // 返回其值为 (this % val) 的 BigInteger                 。 public BigInteger pow(int exponent); // 返回其值为 (thisexponent) 的 BigInteger            。注意,exponent 是一个整数而不是 BigInteger      。

同样的是运算后返回一个新的 BigInteger对象                  ,不是在原来的基础上修改的

举例

import java.math.BigInteger; public class BigIntegerTest { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("-3"); BigInteger abs = bigInteger.abs(); // 绝对值 System.out.println(abs); BigInteger bigInteger2 = new BigInteger("2"); BigInteger remainder = bigInteger.remainder(bigInteger2); // 取模 % System.out.println(remainder); BigInteger pow = bigInteger.pow(2); // 次方 System.out.println(pow); } }

2. BigDecimal

一般的 Float 类 和 Double 类可以用来左科学计算或工程计算                 ,但在商业,财务            ,金融 计算中                 ,要求的数字精度比较高      ,故用

到 java.math.BigDecimal 类                 。

因为 无论是 Float 类 还是 Dobule 类都存在精度问题            。具体原因大家可以移步至: 浮点数的精确度的探究_ChinaRainbowSea的博客-CSDN博客            ,

你真的了解C语言 if - else             、bool(布尔值)                 、浮点数损失吗 ?_c11中有bool_ChinaRainbowSea的博客-CSDN博客

如下:

public class BigDoubleTest { public static void main(String[] args) { double num = 2.9999999999999999999998; System.out.println(num); } }

BigDecimal类解决了这个                 ,精度上的问题      ,并支持不可变的      、任意精度的有符号十进制定点数。

常用的构造器两个

public BigDecimal(double val); // 将 double 转换为 BigDecimal      ,后者是 double 的二进制浮点值准确的十进制表示形式                 。 public BigDecimal(String val); // 将 BigDecimal 的字符串表示形式转换为 BigDecimal

举例:

import java.math.BigDecimal; public class BigDoubleTest { public static void main(String[] args) { double num = 2.9999999999999999999998; System.out.println(num); // 精度问题; BigDecimal bigDecimal2 = new BigDecimal("2.9999999999999999999998"); System.out.println(bigDecimal2); } }

2. 1 BigDecimal 常用的方法

2.1.1 BigDecimal 的                  ”+“ add(), "-"subtract                 ,"*" multiply           ,"/" divide public BigDecimal add(BigDecimal augend); // 返回一个 BigDecimal      ,其值为 (this + augend)                  ,其标度为 max(this.scale(), augend.scale())                  。 public BigDecimal divide(BigDecimal divisor,int scale,RoundingMode roundingMode) // 返回一个 BigDecimal           ,其值为 (this / divisor),其标度为指定标度。如果必须执行舍入                  ,以生成具有指定标度的结果                 ,则应用指定的舍入模式           。 参数: divisor - 此 BigDecimal 要除以的值                  。 scale - 要返回的 BigDecimal 商的标度      。 roundingMode - 要应用的舍入模式           。 public BigDecimal multiply(BigDecimal multiplicand); // 返回一个 BigDecimal,其值为 (this × multiplicand)            ,其标度为 (this.scale() + multiplicand.scale())                 。 public BigDecimal subtract(BigDecimal subtrahend); // 返回一个 BigDecimal                 ,其值为 (this - subtrahend)      ,其标度为 max(this.scale(), subtrahend.scale())      。

注意: 是不可变的            ,都是计算后返回一个新的BigDecimal 对象                 ,不是在原来的基础上修改的      。

举例;

import java.math.BigDecimal; public class BigDoubleTest { public static void main(String[] args) { BigDecimal bigDecimal = new BigDecimal("2.9999999999999999999998"); BigDecimal add = bigDecimal.add(new BigDecimal("1")); // 加 System.out.println(add); BigDecimal subtract = bigDecimal.subtract(new BigDecimal(1)); // 减 System.out.println(subtract); BigDecimal multiply = bigDecimal.multiply(new BigDecimal(2)); // 乘 System.out.println(multiply); BigDecimal divide = bigDecimal.divide(new BigDecimal(2)); // 除 System.out.println(divide); } }

这里特别说明一下 BigDecimal 的**除法**** 会 如果除不尽话      ,可以设置保留精度**

如果除数是 0       ,同样是会报算术异常的                 。

public BigDecimal divide(BigDecimal divisor,int scale, int roundingMode)

如下是: roundingMode 舍入的模式:说白了就是:四舍五入时的取舍规则                 ,什么情况取           ,什么情况舍            。

举例:

import java.math.BigDecimal; public class BigDoubleTest { public static void main(String[] args) { BigDecimal bigDecimal = new BigDecimal("2.9999999999999999999998"); BigDecimal bigDecimal2 = new BigDecimal(2); BigDecimal divide = bigDecimal.divide(bigDecimal2,6,BigDecimal.ROUND_FLOOR); // 除 System.out.println(divide); } }

3. 总结:

BigDecimal 属于大数据      ,精度很高                  ,不属于基本数据类型           ,属于java对象(引用数据类型)这是sun提供的一个类,专门用在财务软件当中      。 注意: 财务软件中的double 是不够的                  ,当处理财务数据时                 ,用那一种类型 ? 千万不能用 double 要用 java.math.BigDecimal 中的类的这个对象 以及处理 Double 精度上的问题,使用 ``java.math.BigDecimal` 处理                 。 对于存储大的 Int 数值 使用 ``java.Math.BigInteger` 类 存储范围更大            。 无论是 : BigDecimal 还是 BigInteger 都是引用类型            ,不是基本数据类型                 ,对于       ”加减乘除“ 运算符      ,都不可以直接运算            ,而是通过 对于的方法进行了。      ”+“ add(), "-"subtract                 ,"*" multiply      ,"/" divide                 。 无论是 : BigDecimal 还是 BigInteger 都是不可变的      ,其计算的结果都是                 ,返回一个新的对象           ,不是在原来的基础上修改的                  。

4. 最后:

限于自身水平      ,其中存在的错误                  ,希望大家给予指教           ,韩信点兵——多多益善,谢谢大家                  ,江湖再见                 ,后会有期 !!!

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
better call saul百科(better sqlite3安装node gyp原生模块编译prebuild-install)