Java大数BigInteger和BigDecimal

BigInteger

static BigInteger ONE ——BigInteger恒一。
static BigInteger TEN ——BigInteger常数十。
static BigInteger ZERO——BigInteger恒为零。

构造方法:

BigInteger​(String val)——将BigInteger的十进制String表示形式转换为BigInteger。

方法:

int intValue​() ——将此BigInteger转换为int。
static BigInteger valueOf(long val) 返回一个BigInteger其值等于指定的 long。

BigInteger add(BigInteger val) ——返回的值是 (this + val) BigInteger。
BigInteger subtract(BigInteger val)——返回的值是 (this - val) BigInteger。
BigInteger multiply​(BigInteger val)——返回值为的BigInteger (this * val)。
BigInteger divide(BigInteger val) ——返回的值是 (this / val) BigInteger。

boolean isProbablePrime——>true如果此BigInteger可能是素数,false则返回.

int compareTo(BigInteger val) 这个BigInteger与指定BigInteger比较。结果返回1、-1、0
boolean equals(Object x) 这与平等BigInteger指定对象比较。

String toString() 返回此BigInteger十进制字符串表示形式。
String toString(int radix) 返回在给定的基本BigInteger的字符串表示形式。

BigDecimal

该类提供了BigDecimal舍入行为的用户完全控制。如果没有指定舍入模式,无法表示准确结果,则抛出一个异常(If no rounding mode is specified and the exact result cannot be represented, an exception is thrown)

Add max(addend.scale(), augend.scale())
Subtract max(minuend.scale(), subtrahend.scale())
Multiply multiplier.scale() + multiplicand.scale()
Divide dividend.scale() - divisor.scale()
在这里插入图片描述

  1. BigDecimal.ROUND_HALF_DOWN最贴切的说法应该是叫五舍六入,舍弃的部分如果大于5才进位,小于或等于5直接舍弃。
  2. BigDecimal.ROUND_HALF_UP就是我们小学教的四舍五入,舍弃的部分如果大于等于5就进位,小于5的直接舍弃。

构造方法:

BigDecimal(BigInteger val) ——Translates a BigInteger into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale) ——Translates a BigInteger unscaled value and an int scale into a BigDecimal.
BigDecimal(String val) ——Translates the string representation of a BigDecimal into a BigDecimal.

方法:

BigDecimal add(BigDecimal augend) ——Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).
——add(BigDecimal augend, MathContext mc) ——返回的值是 BigDecimal (this + augend),舍入根据语境的设置。

BigDecimal subtract(BigDecimal subtrahend) ——Returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).

BigDecimal multiply(BigDecimal multiplicand) ——Returns a BigDecimal whose value is (this ×
multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

BigDecimal divide(BigDecimal divisor, int roundingMode) ——Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

BigInteger toBigInteger() ——Converts this BigDecimal to a BigInteger.

String toPlainString() ——Returns a string representation of this BigDecimal without an exponent field. (an exponent field. 指数域)
String toString() ——Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

toString()在某些情况下显示数值会变为科学计数法,而toPlainString()始终精确显示数值本身。doubleValue()变为科学计数法

end.

猜你喜欢

转载自blog.csdn.net/weixin_44998686/article/details/108862542
今日推荐