版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Java的Math类
import static java.lang.Math.*;//加上这个包以下的Math均可省略 //静态导入
public class testMath {
public static void main(String[] args) {
System.out.println("绝对值 "+Math.abs(23)+"\t"+Math.abs(-23)+"\t"+Math.abs(0));
System.out.println("向上取整再转double "+Math.ceil(23.00001)+"\t"+Math.ceil(-9.99999));
System.out.println("向下取整再转double "+Math.floor(23.99999)+"\t"+Math.floor(-23.00001));
System.out.println("最值:"+Math.max(20, 10)+"\t"+Math.min(3,40));
System.out.println("5的2次方 "+Math.pow(5, 2));
System.out.println("随机数(0-1)"+Math.random());
int ran=(int) (Math.random()*9000);//四位数随机数
System.out.println("四舍五入"+Math.round(34.567)+"\t"+Math.round(34.234));
System.out.println("开方"+Math.sqrt(4));
}
}