四舍五入 取整:
Math.ceil()
|
功能:对一个数进行上取整。
语法:Math.ceil(x)
输出结果为:
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
2, 2, -1, -1
Math.floor()
|
功能:对一个数进行下取整。
语法:Math.floor(x)
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
1, 1, -2, -2 | |
Math.round()
|
功能:四舍五入取整。
语法:Math.round(x)
总结的口诀是:四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一!
Math.Round(3.64, 1) = 3.6 Math.Round(3.65, 1) = 3.6 Math.Round(3.66, 1) = 3.7
返回值
|
1.丢弃小数部分,保留整数部分
js:parseInt(7/2)
2.向上取整,有小数就整数部分加1
js: Math.ceil(7/2)
3,四舍五入.
js: Math.round(7/2)
4,向下取整
js: Math.floor(7/2) |