js絕對(duì)值函數(shù)的使用abs()
編寫方式:
Math.abs(-1);
其中-1為要求絕對(duì)值的數(shù)值。
注意:
在求取一個(gè)數(shù)值的絕對(duì)值時(shí)可以使用abs函數(shù),由于此函數(shù)屬于Math類,所有在前邊要加上Math.。
范例:
- <html>
- <head>
- <title>絕對(duì)值函數(shù)的使用</title>
- </head>
- <body>
- <script language="javascript">
- <!--
- document.write("0的絕對(duì)值為:",Math.abs(0),"<br>");
- document.write("1的絕對(duì)值為:",Math.abs(1),"<br>");
- document.write("-1的絕對(duì)值為:",Math.abs(-1),"<br>");
- //-->
- </script>
- </body>
- </html>
新聞熱點(diǎn)
疑難解答