Math
数学工具类,提供一些对数据进行操作的静态方法
Math类常用API
方法名 | 说明 |
---|---|
public static int abs(int a) | 获取参数绝对值 |
public static double ceil(double a) | 向上取整 |
public static double floor(double a) | 向下取整 |
public static int round(float a) | 四舍五入 |
public static int max(int a, int b) | 获取两个int值中的最大值 |
public static double pow(double a, double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的随机值,范围[0.0, 1.0) |
案例演示
public class MathTest {public static void main(String[] args) {//abs()取绝对值System.out.println(Math.abs(-33.5)); //33.5//ceil()向上取整System.out.println("-------------------");System.out.println(Math.ceil(26.5)); //27.0System.out.println(Math.ceil(-55.6)); //-55.0//floor()向下取整System.out.println("-------------------");System.out.println(Math.floor(12.3)); //12.0System.out.println(Math.floor(-25.7)); //-26.0//round()四舍五入System.out.println("-------------------");System.out.println(Math.round(27.8)); //28System.out.println(Math.round(27.4)); //27//max()两数最大值,min()两数最小值System.out.println("-------------------");System.out.println(Math.max(35, 34.5)); //35.0 自动类型提升System.out.println(Math.min(12.3, 50)); //12.3//pow()取次方System.out.println("-------------------");System.out.println(Math.pow(2, 3)); //8.0//random()随机数System.out.println("-------------------");System.out.println((int)(Math.random() * 11)); //取[0, 10]之间的随机数}
}