Math类常用方法
静态字段:
static double E
:比任何其他值都接近e(即自然对数的底数)的double值。
static double PI
:比任何其他值都接近pi的double值。
方法:
static double abs(int||double||folat||long a)
:返回对应类型值的绝对值。
static double ceil(double a)
:向上取整。
static double floor(double a)
:向下取整。
static long round(double a)
:返回最接近参数的long值。
static int round(float a)
:返回最接近参数的int值。
static float/int/double/long max(float||... a,float||... b)
:返回两个参数中较大的一个。
static float/int/double/long min(float||... a,float||... b)
:返回两个参数中较小的一个。
static double random()
:随机返回带正号的double值,该值大于0.0小于1.0.
public static void main(String[] args) {
// Math.E自然对数
System.out.println(Math.E);
// int类型的运算结果继续为int
System.out.println(1 / 2);
// 圆周率
System.out.println(Math.PI);
// Math.round(number)对给定的number进行四舍五入
System.out.println(Math.round(-10.4));
// Math.floor(number)对number向下取整
System.out.println(Math.floor(10.9));
// Math.ceil(number)对于number向上取整
System.out.println(Math.ceil(10.1));
// Math.sqrt(number)对number进行求平方根
System.out.println(Math.sqrt(9));
// Math.pow(n1, n2)求n1的n2次方
System.out.println(Math.pow(2, 3));
// Math.sin(number)求number的正弦值
System.out.println(Math.sin(Math.PI / 2));
}