java 中的 Math.round(-1.5) 等于多少?
答:-1。
Math.round()四舍五入的原理,小数:
大于0.5,舍去小数,绝对值+1;
小于0.5,仅舍去小数;
等于0.5,取原数字+0.5
Math.floor() 求一个最接近它的整数,它的值小于或等于这个浮点数。(正数去掉小数,负数去小数+1)
示例:
Math.floor( 45.95);
// 45
Math.floor( 45.05);
// 45
Math.floor( 4 );
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46