Home > Web Front-end > JS Tutorial > body text

javascript abs 方法计算数字的绝对值

WBOY
Release: 2016-06-01 09:54:40
Original
1530 people have browsed it

abs 方法基本语法

<code class="language-javascript">Math.abs(number)</code>
Copy after login

参数介绍 

参数 描述
number 必需。abs 方法要需要计算绝对值的数值表达式

返回值

返回的值是 number 参数的绝对值。

实例

下面的实例演示了abs 方法的用法。

<code class="language-javascript">function ComparePosNegVal(n)
{
   var s;
   var v1 = Math.abs(n);
   var v2 = Math.abs(-n);
   if (v1 == v2)
      s = n + " 和 "
      s += -n + " 的绝对值是相同的。";
   return(s);
}
document.write(ComparePosNegVal(7));</code>
Copy after login

在线运行

运行结果为:

<code class="language-javascript">7 和 -7 的绝对值是相同的。</code>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!