java Math类的常用方法

本文详细介绍了Java中Math类的各种常用方法,包括四舍五入、取整、随机数生成、求最大最小值、绝对值、平方根、立方根、幂运算等,并提供了具体的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java Math类的常用方法

    public void mathTest(){

        /**
         * Math.rint()
         * 四舍五入,返回double值
         * 小数点后为.5的时候不一定是四舍五入,而是取邻近的偶数
         */
        System.out.println(Math.rint(1.3));         //1.0
        System.out.println(Math.rint(1.5));         //2.0
        System.out.println(Math.rint(2.5));         //2.0
        System.out.println(Math.rint(-2.3));         //-2.0

        System.out.println("-----------------------------");

        /**
         * Math.round()
         * 四舍五入
         * float类型返回int,double类型返回long
         */
        System.out.println(Math.round(1.5f));         //2(int型)
        System.out.println(Math.round(1.5));         //2(long型)
        System.out.println(Math.round(1.3f));         //1(int型)
        System.out.println(Math.round(-1.3));         //-1(long型)

        System.out.println("-----------------------------");

        /**
         * Math.floor()
         * 向下取整
         * 返回值为double类型
         */
        System.out.println(Math.floor(1.2));         //1.0
        System.out.println(Math.floor(-3.3));         //-4.0

        System.out.println("-----------------------------");

        /**
         * Math.ceil()
         * 向上取整
         * 返回值为double类型
         */
        System.out.println(Math.ceil(1.3));          //2.0
        System.out.println(Math.ceil(-3.8));          //-3.0

        System.out.println("-----------------------------");

        /**
         * Math.random()
         * 返回0-1之间的伪随机数,返回值为double类型
         */
        System.out.println(Math.random());          //0-1之间的伪随机数

        System.out.println("-----------------------------");

        /**
         * Math.max(p1,p2)
         * 返回两数中的最大值
         */
        System.out.println(Math.max(1, 3));       //3

        System.out.println("-----------------------------");

        /**
         * Math.min(p1,p2)
         * 返回两数中的最小值
         */
        System.out.println(Math.min(1, 3));       //1

        System.out.println("-----------------------------");

        /**
         * Math.abs(p1)
         * 求绝对值
         */
        System.out.println(Math.abs(-4));       //4
        System.out.println(Math.abs(4));       //4

        System.out.println("-----------------------------");

        /**
         * Math.sqrt(p1)
         * 求平方根
         * 返回值为double类型
         */
        System.out.println(Math.sqrt(4));       //2.0

        System.out.println("-----------------------------");

        /**
         * Math.cbrt(p1)
         * 求立方根
         * 返回值为double类型
         */
        System.out.println(Math.cbrt(27));       //3.0

        System.out.println("-----------------------------");

        /**
         * Math.pow(p1,p2)
         * 求p1的p2次方
         * 返回值为double类型
         */
        System.out.println(Math.pow(2,3));       //8.0

        System.out.println("-----------------------------");
        
        //除以上常用方法外,Math方法还有cos(), sin()等方法

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值