Math类中提供了5个与取整相关的函数,如下所示:
注意这些在c++里也适用
static double ceil(double a):天花板函数,返回大于等于a的最小整数(但是以浮点数形式存储)。
static double floor(double a):地板函数,返回小于等于a的最大整数(但是以浮点数形式存储)。
static double rint(double a):四舍五入函数,返回与a的值最相近的整数(但是以浮点数形式存储)。
static long round(double a):四舍五入函数,返回与a的值最相近的长整型数。
static int round(float a):四舍五入函数,返回与a的值最相近的整型数。
调用方式:
Math.rint(3.5); //得出4.0
算术计算
Math.sqrt(): 计算平方根Math.cbrt(): 计算立方根Math.pow(a, b): 计算a的b次方Math.max( , ): 计算最大值Math.min( , ): 计算最小值Math.abs(): 取绝对值
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.cbrt(8)); // 2.0
System.out.println(Math.pow(3, 2)); // 9.0
System.out.println(Math.max(2.3, 4.5));// 4.5
System.out.println(Math.min(2.3, 4.5));// 2.3
/**
* abs求绝对值
*/
System.out.println(Math.abs(-10.4)); // 10.4
System.out.println(Math.abs(10.1)); // 10.1