Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Buộc hiển thị số theo ký pháp mũ (exponential) với toExponential()

Phương thức toExponential() chuyển số thành chuỗi ký pháp khoa học (mũ), dù số nằm trong phạm vi hiển thị thường.

Tham số:

  • fractionDigits (tùy chọn) – Số chữ số sau dấu thập phân.

Ví dụ

<html>
  <head>
    <title>JavaScript toExponential() Method</title>
  </head>
  <body>
    <script>
      var num = 77.1234;
      document.write("toExponential(): " + num.toExponential() + "<br>");   // 7.71234e+1
      document.write("toExponential(4): " + num.toExponential(4) + "<br>"); // 7.7123e+1
      document.write("toExponential(2): " + num.toExponential(2) + "<br>"); // 7.71e+1
    </script>
  </body>
</html>