round方法怎么使用-LMLPHP

下面我们来看round方法的具体使用方法。

首先我们来看一下round方法的基本语法

Math.round(x);
登录后复制

x表示需要四舍五入的数字。

我们来看具体示例

代码如下

<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body> 
<script type="text/javascript"> 
    var value = Math.round( 0.5 );
         document.write("0.5四舍五入的值为:" + value ); 
         
         var value = Math.round( 20.7 );
         document.write("<br />20.7四舍五入的值为: " + value ); 
         
         var value = Math.round( 20.3 );
         document.write("<br />20.3四舍五入的值为:" + value ); 
         
         var value = Math.round( -20.3 );
         document.write("<br />-20.3四舍五入的值为:" + value ); 
</script> 
   </body>
</html>
登录后复制

运行结果如下图所示

round方法怎么使用-LMLPHP

我们可以看到上述代码的数字的结果都是四舍五入到最接近的整数。

本篇文章到这里就全部结束了,更多精彩内容大家可以关注Work网的其他栏目教程!!!

以上就是round方法怎么使用的详细内容,更多请关注Work网其它相关文章!

09-16 17:43