1.Math.round():
//该方法为重载方法
static long round(double a)
static int round(float a)
返回最接近参数a的整数,该方法等同于Math.floor(a + 0.5)并将结果转换为long或int类型,即四舍五入取整。
小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11
小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12
小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11 (注意)
总结:(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。
2.Math.ceil():偏向于最近的较大的整数
例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
3.Math.floor():偏向于最近的较小的整数
例如:
Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12