对负数进行 round() 运算时,当绝对值小于0.5,直接返回0; 如果绝对值大于0.5,先+0.5,然后做floor()运算。 下面是源码: 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(g);
运行结果,i和j的值都是 -9,k的值为 -10,floor(-9.1)的值为-10