Math类常用方法详解
总体方法一览
abs
返回绝对值
有如下几个重载形式:
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
1 |
|
pow
求幂函数
public static double pow(double a, double b)
返回值为ab
1 |
|
ceil
向上取整
public static double ceil(double a)
1 |
|
floor
向下取整
public static double floor(double a)
1 |
|
round
四舍五入
有两个重载形式
public static long round(double a)
public static int round(float a)
1 |
|
sqrt
求开方
public static double sqrt(double a)
1 |
|
max
求两个数的最大值
有四种重载形式
public static int max(int a, int b)
public static long max(long a, long b)
public static float max(float a, float b)
public static double max(double a, double b)
1 |
|
min
求两个数的最小值
有四种重载形式
public static int min(int a, int b)
public static long min(long a, long b)
public static float min(float a, float b)
public static double min(double a, double b)
1 |
|
random
求随机数
public static double random()
这个方法返回值为[0,1)的小数
求[a, b)范围的随机数:a + Math.random() * (b - a + 1)
1 |
|