首页 > 开发 > JS > 正文

js 绝对值函数abs

2024-05-06 16:29:27
字体:
来源:转载
供稿:网友

JS中通过Math对象的abs函数获取数字的绝对值,abs()函数的语法如下:
1 Math.abs(x); 下面来看一些abs函数简单的范例:

  1. 01 <script language='JavaScript' type='text/JavaScript'
  2. 02 <!-- 
  3. 03 document.write(Math.abs(-5)); // output is 5 
  4. 04 document.write("<br>"); 
  5. 05 document.write(Math.abs(5.56)); // output is 5.56 
  6. 06 document.write("<br>"); 
  7. 07 document.write(Math.abs(-2.5)); // output is 2.5 
  8. 08 document.write("<br>"); 
  9. 09 document.write(Math.abs(-1.355)); // output is 1.355 
  10. 10 document.write("<br>"); 
  11. 11 //--> 
  12. 12 </script> 



abs() 函数的参数不但可以是具体的数字也可以是一个算术表达式:
 

  1. document.write(Math.abs(43-64)); // output is 21 

注:相关教程知识阅读请移步到JavaScript/Ajax教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表