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

Làm thế nào để buộc một số hiển thị dưới dạng ký hiệu hàm mũ?


Sử dụng toExponential () để buộc một số hiển thị dưới dạng ký hiệu hàm mũ, ngay cả khi số đó nằm trong phạm vi mà JavaScript thường sử dụng ký hiệu chuẩn.

Sau đây là tham số -

  • fractionDigits - Một số nguyên xác định số chữ số sau dấu thập phân. Mặc định bao nhiêu chữ số cần thiết để chỉ định số.

Ví dụ

Bạn có thể thử chạy đoạn mã sau để buộc một số hiển thị ở dạng hàm mũ -

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