对负数进行 round() 运算:绝对值小于0.5,直接返回0;绝对值大于0.5,先将原值 +0.5,然后对其做 floor() 运算
负数的floor():如果小数部分为0,则取整数部分;如果小数部分不为0,则取 整数部分-1
下面是源码:
* public static int round(float a) { * // greatest float value less than 0.5 * if (a != 0x1.fffffep-2f) * return (int)floor(a + 0.5f); * else * return 0;
float f = -9.4f; int i = Math.round(f);
float g = -9.5f; int j = Math.round(g);
float h = -9.6f; int k = Math.round(h);
运行结果,i和j的值都是 -9,k的值为 -10,floor(-9.1)的值为-10