JavaScript abs() 方法


定义和用法

abs() 方法可返回数的绝对值。

语法
  1. Math.abs(x)
参数 描述
x 必需。必须是一个数值。
返回值

x 的绝对值。


实例

在本例中,我将取得正数和负数的绝对值:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. document.write(Math.abs(7.25) + "<br />")
  5. document.write(Math.abs(-7.25) + "<br />")
  6. document.write(Math.abs(7.25-10))
  7. </script>
  8. </body>
  9. </html>

输出:

  1. 7.25
  2. 7.25
  3. 2.75

分类导航