ฟังก์ชัน toFixed() ของออบเจกต์ Number ยอมรับตัวเลขที่แสดงจำนวนจุดที่จะแสดงหลังทศนิยมและแสดงตัวเลขปัจจุบันตามลำดับ
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
num.toFixed(num);
ตัวอย่าง
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var num = Math.PI;
result = num.toFixed(4);
document.write("Fixed point notation of the given number is: " + result);
</script>
</body>
</html> ผลลัพธ์
Fixed point notation of the given number is: 3.1416
ตัวอย่าง
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var num = Math.PI;
document.write("Fixed point notation of the given number is: " + num.toFixed(4));
document.write("<br>");
var num = 2.13e+15;
document.write("Fixed point notation of the given number is: " + num.toFixed(4));
</script>
</body>
</html> ผลลัพธ์
Fixed point notation of the given number is: 3.1416 Fixed point notation of the given number is: 2130000000000000.0000