Java中的数学方法

时间:2020-02-01 00:53:25   收藏:0   阅读:100

直接用代码

 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 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!