js取整数、取余数的方法

1、丢弃小数部分,保留整数部分:parseInt()

2、向上取整,有小数就整数部分加1:Math.ceil()

3、四舍五入:Math.round()

4、向下取整:Math.floor()

 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>

 

原文地址:https://www.cnblogs.com/cq-0715/p/9821003.html