使用Math.round()方法获取四舍五入到最接近整数的数字的值。
您可以尝试运行以下代码以将数字的值四舍五入为JavaScript中最接近的整数-
<html> <head> <title>JavaScript Math round() Method</title> </head> <body> <script> var value = Math.round( 0.5 ); document.write("First Test Value : " + value ); var value = Math.round( 20.7 ); document.write("<br />Second Test Value : " + value ); var value = Math.round( 20.3 ); document.write("<br />Third Test Value : " + value ); var value = Math.round( -20.3 ); document.write("<br />Fourth Test Value : " + value ); </script> </body> </html>