Hàm toàn cục parseFloat() phân tích một chuỗi và trả về số thực (floating point number). Hàm đọc từ đầu chuỗi cho đến khi gặp ký tự không hợp lệ, bỏ qua khoảng trắng đầu chuỗi.
Cú pháp
Cú pháp của nó như sau:
parseFloat(string)
Ví dụ
<html> <head> <title>Ví dụ JavaScript</title> </head> <body> <script type="text/javascript"> var result1 = parseFloat(Math.PI); document.write("Kết quả: "+result1); document.write('<br>'); var result2 = parseFloat("245.12@welcome"); document.write("Kết quả: "+result2); document.write('<br>'); var result3 = parseFloat("11111100.010"); document.write("Kết quả: "+result3); </script> </body> </html>
Kết quả
Kết quả: 3.141592653589793 Kết quả: 245.12 Kết quả: 11111100.01