如何在 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) { // 正数 if (number > 0) { number = number + 0.5 } // 负数 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 方法返回四舍五入后的最接近整数值。
我们已经学习了如何使用和不使用 Math.round() 方法在 JavaScript 中将数字四舍五入为最接近的整数。第一种方法是标准方法,第二种方法仅在用户不想使用 Math.round() 时才可用。建议使用 Math.round(),因为它是 JavaScript 的内置方法。