设为首页 加入收藏

TOP

财务精度:BigInteger 与 BigDecimal(一)
2023-07-25 21:43:00 】 浏览:39
Tags: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 v
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇System 下一篇学习笔记——CentOS中的帮助命令..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目