BigInteger类
平时使用的数值类型都有一定的长度限制,当我们要运算的数超过了长度限制之后就无法使用了, 这时我们就可以使用BigInteger,他可以装载其他类型表示的任意长度的数值。
//构造方法
BigInteger bigInteger = new BigInteger("10809705710945701710989");
//常用方法
public BigInteger add(BigInteger augend) // 加
public BigInteger subtract(BigInteger subtrahend) // 减
public BigInteger multiply(BigInteger multiplicand) // 乘
public BigInteger divide(BigInteger divisor) // 除
public BigInteger pow(int n)//平方
public BigInteger[] divideAndRemainder(BigInteger val) : 返回除积和余数
BigDecimal类
由于在运算的时候,float类型和double很容易丢失精度,比如1/2结果为0.49999999;不可变的、任意精度的有符号十进制数。
//构造方法
BigDecimal bigDecimal = new BigDecimal("2.0");
//常用方法
public BigDecimal add(BigDecimal augend) //加
public BigDecimal subtract(BigDecimal subtrahend) // 减
public BigDecimal multiply(BigDecimal multiplicand) // 乘
public BigDecimal divide(BigDecimal divisor) // 除
public BigInteger pow(int n) //平方
//newScale表示保留几位小数,RoundingMode直接调用静态方法
public BigDecimal setScale(int newScale, RoundingMode roundingMode)
//RoundingMode的模式
HALF_DOWN //五舍六入
HALF_UP //四舍五入
CEILING
FLOOR
DecimalFormat类
按照一定的格式输出。
//三位小数,四舍五入
DecimalFormat decimalFormat = new DecimalFormat("#.###");
//数字按照一定格式输出
decimalFormat.format("数字");