如何强制数字以指数表示法显示?
htmljavascriptprogramming scripts
使用 toExponential() 方法强制数字以指数表示法显示,即使数字在 JavaScript 通常使用标准表示法的范围内。
以下是参数 −
- fractionDigits - 指定小数点后位数的整数。默认为指定数字所需的位数。
示例
您可以尝试运行以下代码以强制数字以指数函数显示 −
<html> <head> <title>Javascript Method toExponential()</title> </head> <body> <script> var num=77.1234; var val = num.toExponential(); document.write("num.toExponential() is : " + val ); document.write("<br />"); val = num.toExponential(4); document.write("num.toExponential(4) is : " + val ); document.write("<br />"); val = num.toExponential(2); document.write("num.toExponential(2) is : " + val); document.write("<br />"); val = 77.1234.toExponential(); document.write("77.1234.toExponential()is : " + val ); document.write("<br />"); val = 77.1234.toExponential(); document.write("77 .toExponential() is : " + val); </script> </body> </html>