BigInteger与BigDecimal

BigInteger

  • IntegerInteger 类作为 int int的包装类,能存储最大整型值为 231 -1,Long 类也是有限的, 最大为 263 -1。如果 要表示再大的整数,不管是基本据类 型还他们包装都无能为力,更不用说进行运算了。
  • java.math 包的 BigInteger可以表示不变的任意精度整数 。BigInteger 提供 所有 Java Java 的基本整数操作符对应物,并提供 java.lang.Math 的所有相关方法。 另外, BigInteger 还提供以下运算:模术、 GCD 计算、 质数测试素生成位操作以及一些其他。
  • 构造 器
    • BigInteger (String val):根据字符串构建 BigInteger对象

常用方法

public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

BigDecimal

  • 一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类
  • BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

构造器

public BigDecimal(double val)
public BigDecimal(String val)

常用方法

public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
原文地址:https://www.cnblogs.com/zk2020/p/15056264.html