javascript提供了两种相除取整数的方法:math.floor()和math.round()。
math.floor()
math.floor()方法返回小于或等于给定数字的最大整数。该方法可将任何浮点数作为其参数,并返回该数的最大整数值。例如,如果将3.8作为参数传递给math.floor(),则将返回3。如果将-3.8作为参数传递给math.floor(),则将返回-4。
下面是math.floor()方法的代码示例:
let num = 5 / 2;let result = math.floor(num);console.log(result); // 输出结果为 2
在上面的示例中,我们首先将5除以2并将结果存储在变量num中。然后,我们调用math.floor()方法,并将num作为其参数。最后,将math.floor()方法的结果存储在变量result中,并将其打印到控制台中。
math.round()
math.round()方法返回最接近给定数字的整数值。如果小数部分大于或等于.5,则将返回下一个整数。如果小数部分小于.5,则将返回前一个整数。
下面是math.round()方法的代码示例:
let num = 5 / 2;let result = math.round(num);console.log(result); // 输出结果为 3
在上面的示例中,首先将5除以2,并将结果存储在变量num中。然后,我们调用math.round()方法,并将num作为其参数。最后,将math.round()方法的结果存储在变量result中,并将其打印到控制台中。
综上所述,这是javascript中相除取整数的两种方法。math.floor()方法返回小于或等于给定数字的最大整数,而math.round()方法返回最接近给定数字的整数。这两种方法都可以在javascript中相除取整数,具体使用取决于你的需求。
以上就是javascript相除取整数代码是什么的详细内容。
