Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: Math.round();
常用方法:
double a = 12.81;
int b = (int) a;
System.out.println(b);
long c = Math.round(a);
System.out.println(c);
double d = Math.floor(a);
System.out.println(d);
double e = Math.ceil(a);
System.out.println(e);
double x = Math.random();
System.out.println(x);
int y = (int) (Math.random() * 99);
System.out.println(y);
结果:
12
13
12.0
13.0
0.33036435241086914
59