13、Math类简介

Math类概述

在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等。

package com.sutaoyu.usually_class;

public class String_test10 {
    public static void main(String[] args) {
        //圆周率
        System.out.println(Math.PI); //3.141592653589793
        
        //取绝对值
        System.out.println(Math.abs(-10)); //10
        
        //ceil天花板,会向上取值,结果是double
        System.out.println(Math.ceil(12.11));//13.0
        System.out.println(Math.ceil(9.89));//10.0
        
        //floor地板,会向下取整,结果是double
        System.out.println(Math.floor(11.11));//11.0
        System.out.println(Math.floor(7.89));//7.0
        
        //获取两个值中的最大值
        System.out.println(Math.max(100, 80));//100
        
        //前面的数是底数,后面的数是指数,即2的3次方
        System.out.println(Math.pow(2, 3));//8.00
        
        //生成0.0到1.0之间的随机小数,包括0.0,不包括1.0
        System.out.println(Math.random()); //0.7952284902360376
        
        //四舍五入
        System.out.println(Math.round(12.3f));//12
        System.out.println(Math.round(9.8f));//10
        
        //开平方
        System.out.println(Math.sqrt(27));//5.196152422706632
        
    }
}

猜你喜欢

转载自www.cnblogs.com/zhuifeng-mayi/p/10123492.html
今日推荐