toExponential()

优质
小牛编辑
126浏览
2023-12-01

描述 (Description)

此方法以指数表示法返回表示number对象的字符串。

语法 (Syntax)

其语法如下 -

number.toExponential( [fractionDigits] )

参数细节 (Parameter Details)

fractionDigits - 一个整数,指定小数点后的位数。 默认为指定数字所需的位数。

返回值 (Return Value)

一个字符串,以指数表示法表示Number对象,小数点前有一位数,四舍五入到小数点后的fractionDigits数字。 如果省略fractionDigits参数,则小数点后的位数默认为唯一表示该值所需的位数。

例子 (Example)

请尝试以下示例。

<html>
   <head>
      <title>Javascript Method toExponential()</title>
   </head>
   <body>
      <script type="text/javascript">
         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>

输出 (Output)

num.toExponential() is : 7.71234e+1
num.toExponential(4) is : 7.7123e+1
num.toExponential(2) is : 7.71e+1
77.1234.toExponential()is:7.71234e+1
77 .toExponential() is : 7.71234e+1