Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

ฟังก์ชัน parseFloat() ใน JavaScript


ฟังก์ชัน parseFloat() ยอมรับพารามิเตอร์สองตัว อันแรกคือสตริงที่ใช้แทนตัวเลข และอีกตัวคือตัวเลขที่แทนฐานและคืนค่าจำนวนเต็มของฐานที่กำหนด

ไวยากรณ์

ไวยากรณ์ของมันคือดังต่อไปนี้

num.parseFloat('4524', 8);

ตัวอย่าง

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var result1 = parseFloat(Math.PI);
      document.write("Result: "+result1);
      document.write('<br>');
      var result2 = parseFloat("245.12@welcome");
      document.write("Result: "+result2);
      document.write('<br>');
      var result3 = parseFloat("11111100.010");
      document.write("Result: "+result3);
   </script>
</body>
</html>

ผลลัพธ์

Result: 3.141592653589793
Result: 245.12
Result: 11111100.01