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