Math類中提供了5個與取整相關的函數,如下所示:
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