首页 > 代码库 > Java Math类

Java Math类

 1 package demo04;
 2 
 3 //Math类所有方法都是静态方法,直接类名调用
 4 public class MathDemo {
 5     public static void main(String[] args) {
 6         
 7         //static int abs(int a) 返回 int 值的绝对值 
 8         System.out.println(Math.abs(-1));            //1
 9         
10         //static double ceil(double a) 向上取整
11         System.out.println(Math.ceil(5.1));          //6.0
12         
13         //static double floor(double a) 向下取整
14         System.out.println(Math.floor(5.1));         //5.0
15         
16         //static double pow(double a, double b) a的b次方
17         System.out.println(Math.pow(2, 3));          //8.0
18         
19         //static double sqrt(double a) 开方
20         System.out.println(Math.sqrt(4));            //2.0
21         
22         //static double random() 随机数[0.0,1.0)
23         System.out.println(Math.random());
24         
25         //static long round(double a) 四舍五入取整
26         System.out.println(Math.round(1.5));         //2
27          
28     }
29 }
30         

 

Java Math类