文章目录
🐒个人主页
🏅JavaSE系列专栏
📖前言:
本篇博客主要介绍BigInteger类和BigDecimal类的用途及常用方法
🎀BigInteger类和BigDecimal类的由来
在许多科学领域,Long存储(8个字节)的值远远不够用,亟需更大的类型来存储数据。于是Java中提供了一个可以存储任意精度整数的类BigInteger,可以存储任意精度浮点数的类BigDecimal。并且给它们定义了加减乘除
四大方法
add() ,subtract(),multiply(),divide() 支持运算。
🎀BigDecimal类的优点
由于在浮点数在Java中显示为近似数
,而使用BigDecimal类
刚好克服了这一点(计算更精确)
🎀BigDecimal类容易引发的错误
如果两个浮点数的除法得到值为无限循环小数的话,会报异常
🏅处理方法
🎀BigInteger类常用方法:
构造方法 | 方法内容介绍 |
---|---|
BigInteger (byte[] val) | 📖将字节数组转为“大整数形式” |
BigInteger (String val) | 📖将十进制字符串转为“大整数形式” |
常用方法 | 方法内容介绍 |
BigInteger abs () | 📖【绝对值】 |
BigInteger add (BigInteger val) | 📖【加】返回值为 (this + val) |
BigInteger subtract (BigInteger val) | 📖【减】返回值为 (this - val) |
BigInteger multiply (BigInteger val) | 📖【乘】返回值为 (this * val) 。 |
BigInteger divide (BigInteger val) | 📖【除】返回值为 (this / val) |
int compareTo (BigInteger val) | 📖将此BigInteger与指定的BigInteger进行比较 |
double doubleValue () | 📖将此BigInteger转换为 double |
int intValue () | 📖将此BigInteger转换为 int |
long longValue () | 📖将此BigInteger转换为 long |
🎀BigDecimal类常用方法:
构造方法 | 方法内容介绍 |
---|---|
BigDecimal (BigInteger val) | 📖将 BigInteger转换成 BigDecimal |
BigDecimal (char[] in) | 📖一个转换的字符数组表示 BigDecimal |
BigDecimal (double val) | 📖【精确值】将 double转换为 BigDecimal |
BigDecimal (int val) | 📖将 int成 BigDecimal |
BigDecimal (String val) | 📖将BigDecimal的字符串表示 BigDecimal |
常用方法 | 方法内容介绍 |
BigDecimal abs () | 📖返回一个 BigDecimal ,其值为此 BigDecimal的绝对值 |
BigDecimal add (BigDecimal augend) | 📖【加】返回 BigDecimal ,其值是 (this + augend) |
BigDecimal subtract (BigDecimal subtrahend) | 📖【减】返回 BigDecimal ,其值是 (this - subtrahend) |
BigDecimal multiply (BigDecimal multiplicand) | 📖【乘】返回 BigDecimal ,其值是 (this × multiplicand) |
BigDecimal divide (BigDecimal divisor) | 📖【除】返回BigDecimal ,其值为(this / divisor) |
BigDecimal remainder (BigDecimal divisor) | 📖【取余】返回 BigDecimal ,其值是 (this % divisor) |
int compareTo (BigDecimal val) | 📖【比较】将此 BigDecimal与指定的BigDecimal进行 比较 |
BigDecimal movePointLeft (int n) | 📖【左移小数点】返回一个 BigDecimal ,相当于这个小数点,向左移动了 n个地方 |
BigDecimal movePointRight (int n) | 📖【右移小数点】返回一个 BigDecimal ,相当于这个小数点移动了 n个地方 |
BigDecimal pow (int n) | 📖【指数】返回 BigDecimal ,其值是 (thisn) |
BigInteger toBigInteger () | 📖【转BigInteger】将此 BigDecimal转换为 BigInteger |
double doubleValue () | 📖将此 BigDecimal转换为 double |
int intValue () | 📖将此 BigDecimal转换为 int |