js取整數、取餘數的方法

一、丟棄小數部分,保留整數部分:parseInt()spa

二、向上取整,有小數就整數部分加1:Math.ceil()code

三、四捨五入:Math.round()blog

四、向下取整:Math.floor()ip

 1 <script>
 2     // 向上取整
 3     var ceil = Math.ceil(5 / 2)
 4     console.log('++++++++++', ceil) // 3
 5 
 6     // 向下取整
 7     var floor = Math.floor(5 / 2)
 8     console.log('----------', floor) // 2
 9 
10     // 四捨五入
11     var round1 = Math.round(5 / 2),
12     round2 = Math.round(10 / 3)
13     console.log('**********', round1) // 3
14     console.log('**********', round2) // 3
15   </script>

 

相關文章
相關標籤/搜索