เมธอด toExponential() คืน string แทนตัวเลขในรูปแบบ Exponential notation (เลขยกกำลัง)
Syntax
number.toExponential([fractionDigits])
fractionDigits (optional) – จำนวนหลักหลังจุดทศนิยม (0-20)
ตัวอย่าง
<html>
<head>
<title>JavaScript Method toExponential()</title>
</head>
<body>
<script>
var num = 77.1234;
document.write("num.toExponential(): " + num.toExponential() + "<br>"); // 7.71234e+1
document.write("num.toExponential(4): " + num.toExponential(4) + "<br>"); // 7.7123e+1
document.write("num.toExponential(2): " + num.toExponential(2) + "<br>"); // 7.71e+1
document.write("77.1234.toExponential(): " + 77.1234.toExponential() + "<br>");
document.write("77..toExponential(): " + 77..toExponential() + "<br>"); // 7.7e+1
</script>
</body>
</html>
รูปแบบ Output
7.71234e+1= 7.71234 × 10¹ = 77.1234e+1หมายถึง × 10¹,e-3= × 10⁻³
เมธอด Number formatting อื่นๆ
| Method | คำอธิบาย | ตัวอย่าง (77.1234) |
|---|---|---|
toFixed(digits) | ทศนิยมคงที่ | "77.12" |
toPrecision(digits) | Significant digits ทั้งหมด | "77.1" (3 digits) |
toExponential(digits) | Scientific notation | "7.71e+1" |
toLocaleString() | รูปแบบตาม locale | "77.123" (th-TH) |
คืนค่าทั้งหมดเป็น String ต้องแปลงกลับด้วย Number() หรือ + ถ้าต้องการคำนวณต่อ