JAVA Math常用方法

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
    }

猜你喜欢

转载自www.cnblogs.com/wyc1991/p/9011510.html