JAVA 常用API 数学相关类

Math类 数学常用工具类

特点

构造私有,方法静态

使用

类名直接调用

方法

求参数的绝对值static int abs(int a)
向上取整static double ceil(double a)
向下取整static double floor(double a)
四舍五入static long round(double a)
求两个数之间的较大值static int max(int a,int b)
求两个数之间的较小值static int min(int a,int b)

public class Demo01Math {
    public static void main(String[] args) {
        //static int abs(int a) -> 求参数的绝对值
        System.out.println(Math.abs(-10));
        //static double ceil(double a) -> 向上取整
        System.out.println(Math.ceil(3.6));
        //static double floor(double a) ->向下取整
        System.out.println(Math.floor(3.6));
        //static long round(double a)  -> 四舍五入
        System.out.println(Math.round(3.6));
        System.out.println(Math.round(-2.8));
        //static int max(int a, int b) ->求两个数之间的较大值
        System.out.println(Math.max(10,20));
        //static int min(int a, int b) ->求两个数之间的较小值
        System.out.println(Math.min(10,20));
    }
}

BigInteger 数学相关类

处理超大整数的类

构造

BigInteger(String s)

方法

返回值为(this+val)的BigIntegerBigInteger add(BigInteger val)
返回值为(this-val)的BigIntegerBigInteger substract(BigInteger val)
返回值为(this*val)的BigIntegerBigInteger multiply(BigInteger val)
返回值为(this/val)的BigIntegerBigInteger divide(BigInteger val)

public class Demo02BigInteger {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("121212121212121212121212121212121212121");
        BigInteger b2 = new BigInteger("121212121212121212121212121212121212121");
        //BigInteger add(BigInteger val)  返回其值为 (this + val) 的 BigInteger
        System.out.println(b1.add(b2));
        //BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger
        System.out.println(b1.subtract(b2));
        //BigInteger multiply(BigInteger val)  返回其值为 (this * val) 的 BigInteger
        System.out.println(b1.multiply(b2));
        //BigInteger divide(BigInteger val)    返回其值为 (this / val) 的 BigInteger
        System.out.println(b1.divide(b2));
    }
}

int intValue() 将BigInteger转成int

long longValue() 将BigInteger 转成 long

BigDecimal 数学相关类

解决用float和double直接做运算而产生的精度损失问题的类

构造

1BigDecimal(String num)num必须是数字形式

方法

初始化小数时可以传入double型数据static BigDecimal valueOf(double val)
返回值为(this+val)的BigDecimalBigDecimal add(BigDecimal val)
返回值为(this-val)的BigDecimalBigDecimal substract(BigDecimal val)
返回值为(this*val)的BigDecimalBigDecimal multiply(BigDecimal val)
返回值为(this/val)的BigDecimalBigDecimal divide(BigDecimal val)

BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

divisor:除数 除号后面的那个数
scale:指定保留几位小数
roundingMode:取舍方式

roundingMode:取舍方式
	static int ROUND_UP; //向上加1
	static int ROUND_DOWN; //直接舍去
	static int ROUND_HALF_UP; //四舍五入

注意:如果除不尽,会出现运算异常

注意:如果调用的成员上面有一个横线,证明此成员过时了,底层会有一个注解@Deprecated修饰,但是过时的成员还能使用,只不过被新的成员代替了,不推荐使用了

divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 
         divisor:代表除号后面的数据
         scale:保留几位小数
         roundingMode:取舍方式-> RoundingMode是一个枚举,里面的成员可以类名直接调用
                                UP:向上加1 
                                DOWN:直接舍去 
                                HALF_UP:四舍五入 
    private static void big04() {
        BigDecimal b1 = new BigDecimal("3.55");
        BigDecimal b2 = new BigDecimal("2.12");
        BigDecimal divide = b1.divide(b2, 2, RoundingMode.HALF_UP);
        System.out.println("divide = " + divide);
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值