-
Math:数学工具类,有很多数学相关运算的方法。如基本指数,对数,平方根和三角函数。
//static double abs(double a)返回值为 double绝对值。//static double ceil(double a) 天花板,向上取整 System.out.println(Math.ceil(3.14));//4.0 //static double floor(double a) 地板,向下取整 System.out.println(Math.floor(3.14));//3.0 //static long/int round(double a) 四舍五入 System.out.println(Math.round(3.45));//4 //static double max(double a, double b)返回两个 double值中的较大值。 //static double min(double a, double b)返回两个 double值中的较小值。
-
BigInteger:
比long类型精度更高的整数类型,创建对象用String类型参数的构造方法。//加法:BigDecimal add(BigDecimal augend) 返回 BigDecimal ,其值是 (this + augend) BigDecimal add = bigDecimal.add(bigDecimal2); System.out.println("和:" + add); //减法:BigDecimal subtract(BigDecimal subtrahend) 返回 BigDecimal ,其值是 (this - subtrahend) //乘法:BigDecimal multiply(BigDecimal multiplicand) 返回 BigDecimal ,其值是 (this × multiplicand), //除法:BigDecimal divide(BigDecimal divisor)返回BigDecimal ,其值为(this / divisor /* * 当精度超出BigDecimal表示范围的时候,就需要用divide方法重载方法 * * public BigDecimal divide(BigDecimal divisor,RoundingMode roundingMode) * 该方法现在没有能够设置小数位数的参数,如果需要设置指定小数位数,用重载方法 * RoundingMode:就是一个枚举类,是决定取舍方式的。 * 使用方式:枚举名.字段; * 建议使用:该方法可以决定保留小数的位数 * BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) * scale:保留的小数位数 * roundingMode: 通过类.常量使用,例如:BigDecimal.ROUND_DOWN */ //BigDecimal divide = bigDecimal.divide(bigDecimal2);//ArithmeticException 算数异常 BigDecimal divide = bigDecimal.divide(bigDecimal2, 12, BigDecimal.ROUND_CEILING); System.out.println("商:" + divide);
Math、 BigInteger 常用方法
最新推荐文章于 2024-01-11 10:14:47 发布