如何在 JavaScript 中获取四舍五入到最接近整数的数值?
在本教程中,我们将学习如何在 JavaScript 中获取四舍五入到最接近整数的数值。
在 JavaScript 中,可以通过不同的方法找到数字的最接近整数。在本教程中,我们将看到两种最常用的方法:
使用 Math.round() 方法
不使用 Math.round() 方法
舍入数字意味着从该数字中获取最接近的整数。当您需要近似值来简化计算时,这对于数学计算很有用。
例如:如果您有一个像 0.9999999 这样的数字,那么将此数字用于像乘法或除法这样的数学计算可能是一件具有挑战性的事情。出于这个原因,我们将数字四舍五入到最接近的整数以简化计算。在这种情况下,0.9999999 最接近的数字是 1,因此我们在计算中取 1 而不是 0.9999999。
使用 Math.round() 方法
在 JavaScript 中,Math.round() 方法可用于将数字四舍五入到其最接近的整数。它将一个变量作为参数,该变量预期为数字,并返回该数字的最接近整数。否则,如果参数为空或变量为非数字值,则返回 NaN,表示“非数字”。请看以下示例。
// For Positive numbers Math.round(2.2) // 2 Math.round(2.7) // 3 Math.round(2.5) // 3 // For Negative numbers Math.round(-2.2) // -2 Math.round(-2.7) // -3 Math.round(-2.5) // -2 // For empty argument or Non-numeric value Math.round() // NaN Math.round('abcd') // NaN
语法
用户可以按照以下语法使用 Math.round() 方法获取四舍五入到最接近整数的数值。
Math.round(number)
参数
number − 它接收一个预期为数字的变量。
返回类型
给定数字四舍五入后的最接近整数
NaN − 如果参数为空或为非数字值。
示例
在下面的示例中,我们使用了 Math.round() 方法来获取四舍五入到最接近整数的数值。我们使用了不同的值来观察 Math.round() 方法的输出
<html> <body> <h3>Get the value of a number rounded to the nearest integer using <i>Math.round()</i> method in JavaScript</h3> <div id="root"></div> <script> let number1 = 2.1 let number2 = 2.6 let number3 = 2.5 let number4 = -2.5 let number5 = -2.51 let text = 'abcd' let root = document.getElementById('root') root.innerHTML ='The number ' +number1 +' rounded to the nearest integer: ' +Math.round(number1) + '<br>' root.innerHTML +='The number ' +number2 +' rounded to the nearest integer: ' +Math.round(number2) + '<br>' root.innerHTML +='The number ' +number3 +' rounded to the nearest integer: ' +Math.round(number3) + '<br>' root.innerHTML +='The number ' +number4 +' rounded to the nearest integer: ' +Math.round(number4) + '<br>' root.innerHTML +='The number ' +number5 +' rounded to the nearest integer: ' +Math.round(number5) + '<br>' root.innerHTML +='The text ' +text +' rounded to the nearest integer: ' +Math.round(text) + '<br>' </script> </body> </html>
在上面的输出中,用户可以看到 Math.round() 方法返回数字四舍五入后的最接近整数,对于非数字值,它返回 NaN。
不使用 Math.round() 方法
也可以不使用 Math.round() 方法,通过简单的计算和 ParseInt() 方法找到四舍五入到最接近整数的数字。
ParseInt() 方法接收字符串或任何数字并将其转换为整数;如果字符串是非数字,则返回 NaN。
语法
function round(number) {
// positive number
if (number > 0) {
number = number + 0.5
}
// negative number
else if (number < 0) {
number = number - 0.5
}
return parseInt(number)
}
算法
步骤 1 − 声明一个函数并接收一个数字作为参数。
步骤 2 − 如果数字为正数,则加上 0.5;如果数字为负数,则减去 0.5。
步骤 3 − 使用 parseInt() 并将数字传递给方法,然后返回值
注意 −对于数字 -2.5,此算法返回 -3,而 Math.round(-2.5) 返回 -2。对于所有小数点后为 .5 的数字都一样
示例
在下面的示例中,我们找出了不使用 Math.round() 的情况下四舍五入到最接近整数的数值。我们使用了不同的值来观察输出。
<html> <body> <h4>Get the value of a number rounded to the nearest integer without using<i> Math.round() </i> method in JavaScript</h4> <div id = "root"> </div> <script> let number1 = 2.1 let number2 = -2.6 let number3 = 2.5 let text = 'abcd' function round(number) { if (number > 0) { number = number + 0.5 } else if (number < 0) { number = number - 0.5 } return parseInt(number) } let root = document.getElementById('root') root.innerHTML ='The number ' +number1 +' rounded to the nearest integer: ' + round(number1) + '<br>' root.innerHTML +='The number ' +number2 +' rounded to the nearest integer: ' +round(number2) + '<br>' root.innerHTML +='The number ' +number3 +' rounded to the nearest integer: ' +round(number3) + '<br>' root.innerHTML +='The text ' +text +' rounded to the nearest integer: ' +round(text) + '<br>' </script> </body> </html>
在上面的输出中,用户可以看到 round 方法返回四舍五入后的最接近整数。
我们学习了如何在 JavaScript 中使用和不使用 Math.round() 方法获取四舍五入到最接近整数的数值。第一种方法是标准方法,第二种方法仅在用户不想使用 Math.round() 时才可以使用。建议使用 Math.round(),因为它是在 JavaScript 中执行此操作的内置方法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP