ฟังก์ชัน toPrecision() ของออบเจ็กต์ Number จะรับค่าที่แทนความแม่นยำและคืนค่าการแทนค่าของตัวเลข (ปัจจุบัน) ให้เป็นไปตามความแม่นยำที่ระบุ
ไวยากรณ์
รูปแบบการใช้งานคือดังนี้
num.toPrecision(num);
ตัวอย่าง
<html> <head> <title>ตัวอย่าง JavaScript</title> </head> <body> <script type="text/javascript"> var num = 3325.462; result = num.toPrecision(2); document.write("ผลลัพธ์: " + result); </script> </body> </html>ผลลัพธ์
ผลลัพธ์: 3.3e+3
ตัวอย่าง
<html> <head> <title>ตัวอย่าง JavaScript</title> </head> <body> <script type="text/javascript"> var num = 3325.462; result = num.toPrecision(); document.write("ผลลัพธ์: " + result); </script> </body> </html>ผลลัพธ์
ผลลัพธ์: 3325.462