JAVA MATH类常用方法

来源:互联网 发布:机器人及人工智能大赛 编辑:程序博客网 时间:2024/06/04 18:43

java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在


四舍五入, 随机数,开方,次方,π,自然常数

public class TestNumber {      public static void main(String[] args) {        float f1 = 5.4f;        float f2 = 5.5f;        //5.4四舍五入即5        System.out.println(Math.round(f1));        //5.5四舍五入即6        System.out.println(Math.round(f2));                 //得到一个0-1之间的随机浮点数(取不到1)        System.out.println(Math.random());                 //得到一个0-10之间的随机整数 (取不到10)        System.out.println((int)( Math.random()*10));        //开方        System.out.println(Math.sqrt(9));        //次方(2的4次方)        System.out.println(Math.pow(2,4));                 //π        System.out.println(Math.PI);                 //自然常数        System.out.println(Math.E);    }}


原创粉丝点击