To convert a string
representation of a floating-point number to a number
type, you can use the parseFloat()
function in JavaScript.
Consider this string of floating-point number,
// string with floating point number
const str = "45.67";
Let's use the parseFloat()
function and pass the string
to the function as an argument to convert it to a number
type like this,
// string with floating point number
const str = "45.67";
// convert to number type
const floatNum = parseFloat(str);
console.log(floatNum); // 45.67
- This function is useful when you want to convert values that are obtained from
url
,form
, or whichever places where the values will bestring
type and need to be converted to a floating-point number.
See this example live in JSBin.