数据处理类常用方法

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("数字");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值