JavaScript Code

How to convert string to float?


Problem Statement

given a string value, convert the string to floating point number

Solution

to convert a given string into a floating point number, use JavaScript global function parseFloat(). pass the string as argument to parseFloat() function, and the function returns the floating point value

Program

str is the given string value. we use parseFloat() method to parse the string str into a float

let str = "3.14";
let num = parseFloat(str);
console.log(num);
console.log(typeof(num));