Math類中提供了三個與取整有關的方法:ceil,floor,round,這些方法的作用於它們的英文名稱的含義相對應,例如:ceil的英文意義是天花板,該方法就表示向上取整,Math.ceil(11.3)的結果為12,Math.ceil(-11.6)的結果為-11;floor的英文是地板,該方法就表示向下取整,Math.floor(11.6)的結果是11,Math.floor(-11.4)的結果-12;最難掌握的是round方法,他表示“四舍五入”,算法為Math.floor(x+0.5),即將原來的數字加上0.5后再向下取整,所以,Math.round(11.5)的結果是12,Math.round(-11.5)的結果為-11.
原文地址:http://blog.163.com/linzhijia0612%40126/blog/static/171804664201093553662/