Java常用类-Math、BigInteger、BigDecimal

Math类

java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。

abs     绝对值

acos,asin,atan,cos,sin,tan  三角函数

sqrt     平方根

pow(double a,doble b)     a的b次幂

log    自然对数

exp    e为底指数

max(double a,double b)

min(double a,double b)

random()      返回0.0到1.0的随机数

long round(double a)     double型数据a转换为long型(四舍五入)

toDegrees(double angrad)     弧度—>角度

toRadians(double angdeg)     角度—>弧度

-------------------------------------------------------------------------------------------------------------------------------------

BigInteger类

Integer类作为int的包装类,能存储的最大整型值为2^31-1BigInteger类的数字范围较Integer类的数字范围要大得多,可以支持任意精度的整数。

  • 构造器

           BigInteger(String val)

  •     常用方法
  1.     public BigInteger abs()
  2.     public BigInteger add(BigInteger val)
  3.     public BigInteger  subtract(BigInteger val)
  4.     public BigInteger multiply(BigInteger val)
  5.     public BigInteger divide(BigInteger val)
  6.     public BigInteger remainder(BigInteger val)
  7.     public BigInteger pow(int exponent)
  8.     public BigInteger[] divideAndRemainder(BigInteger val)

--------------------------------------------------------------------------------------------------------------------------------------------------

   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)

              public BigDecimal setScale(int newScale, RoundingMode roundingMode)

public void testBigInteger(){
      BigInteger bi = new BigInteger("12433241123");
      BigDecimal bd = new BigDecimal("12435.351");
      BigDecimal bd2 = new BigDecimal("11");
      System.out.println(bi);
     //System.out.println(bd.divide(bd2));//不指定精度,报错
      System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));
      System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));
}


BigDecimal  setScale(int newScale, RoundingMode roundingMode)

Returns a BigDecimal whose scale(精度) is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

public static final int ROUND_HALF_UP
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school.










 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值