码迷,mamicode.com
首页 > 其他好文 > 详细

Math常用方法

时间:2018-05-08 23:03:48      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   pow   sqrt   天花板   之间   SQ   最小值   11.2   mat   

public class Test {

    public static void main(String[] args) {
        /**
         * Math.sqrt()//计算平方根
         * Math.cbrt()//计算立方根
         * Math.pow(a,b)//计算a的b次方
         * Math.max( , )//计算最大值
         * Math.min( , )//计算最小值        
         */
        
        System.out.println(Math.sqrt(9));//3.0
        System.out.println(Math.cbrt(8));//2.0
        System.out.println(Math.pow(2, 3));//8.0
        System.out.println(Math.max(2.4, 3.8));//3.8
        System.out.println(Math.min(2.5, 8.4));//2.5
        
        /**
         * Math.abs()//求绝对值
         */
        
        System.out.println(Math.abs(-1.5));//1.5
        System.out.println(Math.abs(10.5));//10.5
        
        /**
         * Math.floor()//向下取整,floor是地板的意思;
         * Math.ceil()//向上取整,ceil是天花板的意思;
         * Math.round()//四舍五入取整;
         * Math.rint()//也是四舍五入取整;但.5时会取偶数;
         */
        
        System.out.println(Math.floor(10));//10.0
        System.out.println(Math.floor(10.5));//10.0
        System.out.println(Math.floor(10.7));//10.0
        System.out.println(Math.ceil(11.2));//12.0
        System.out.println(Math.round(10.5));//11
        System.out.println(Math.rint(10.5));//10.0
        
        /**
         * Math.random()//取0到1之间随机的数(包括0但不包括1);
         */
        
        System.out.println(Math.random());//0.0883798391075995
        System.out.println(Math.random() * 2);//0.35747170018174357
        System.out.println(Math.random() + 10);//10.405883407565078
    }

 

Math常用方法

标签:style   pow   sqrt   天花板   之间   SQ   最小值   11.2   mat   

原文地址:https://www.cnblogs.com/wyc1991/p/9011510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!