Computer >> Máy Tính >  >> Lập trình >> Javascript

Phương thức Number.toExponential () trong JavaScript là gì?


Phương thức này trả về một chuỗi đại diện cho đối tượng số trong ký hiệu hàm mũ. Sau đây là cú pháp:

number.toExponential( [fractionDigits] )

Tham số fractionDigits là một số nguyên xác định số chữ số sau dấu thập phân.

Ví dụ

Bạn có thể thử chạy đoạn mã sau để tìm hiểu cách làm việc với phương thức Number.toExponential () trong JavaScript

<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>