Java中的数学方法

直接用代码

 1 public class TestNumber {
 2   
 3     public static void main(String[] args) {
 4         float f1 = 5.4f;
 5         float f2 = 5.5f;
 6         //5.4四舍五入即5
 7         System.out.println(Math.round(f1));
 8         //5.5四舍五入即6
 9         System.out.println(Math.round(f2));
10          
11         //得到一个0-1之间的随机浮点数(取不到1)
12         System.out.println(Math.random());
13          
14         //得到一个0-10之间的随机整数 (取不到10)
15         System.out.println((int)( Math.random()*10));
16         //开方
17         System.out.println(Math.sqrt(9));
18         //次方(2的4次方)
19         System.out.println(Math.pow(2,4));
20          
21         //π
22         System.out.println(Math.PI);
23          
24         //自然常数
25         System.out.println(Math.E);
26     }
27 }

 

posted @ 2020-01-31 23:08  东功  阅读(341)  评论(0编辑  收藏  举报