parseFloat function will convert string to floating point number. parseFloat start parsing the string from first characters till it find all number as number. for example: parseFloat("10") - 10 parseFloat("10.33") - 10.33 parseFloat("34 45 66") - 34 parseFloat(" 60 ") - 60 parseFloat("40 years") - 40 parseFloat("I am 40") -NAN parseFloat("44 am 40") - 44
Javascript parseFloat functions Example - 1
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="convertToFloat()">Parse the string to float</button>
<p id="sample"></p>
<script>
function convertToFloat() {
var a = parseFloat("10") + "<br>";
var b = parseFloat("10.00") + "<br>";
var c = parseFloat("10.33") + "<br>";
var d = parseFloat("34 45 66") + "<br>";
var e = parseFloat(" 60 ") + "<br>";
var f = parseFloat("40 years") + "<br>";
var g = parseFloat("I am 40") + "<br>";
var result = a + b + c + d + e + f + g;
document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>
In this example, We will sum two floating point number and show that in paragraph tag.
See the code snippet:
Javascript parseFloat functions Example - 2
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="convertToFloat()">Parse the string to float</button>
<p id="sample"></p>
<script>
function convertToFloat() {
var a = parseFloat("10");
var b = parseFloat("10.00");
var result = a + b;
document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>