Skip to content

All topics  /  JavaScript

How to convert string into float in JavaScript?

JavaScript ·

If you need to see example of javascript parsefloat: convert string to float number. In this article, we will implement a how to convert string into float in javascript. this example will help you how to convert a string to a floating point number in javascript. let’s discuss about javascript number parsefloat() method in javascript. Here, Creating a basic example of parsefloat() function in javascript.

In JavaScript, you can convert a string to a float using the parseFloat() function. Here is an example:

Example 1:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to convert string into float in JavaScript? - NiceSnippets.Com</title>
</head>
<body>
</body>
<script type="text/javascript">
    var str = "3.14";
    var num = parseFloat(str);
    console.log(num); // Output: 3.14
</script>
</html>

The parseFloat() function takes a string as input and returns a floating-point number. If the string cannot be converted to a number, the function returns NaN (Not a Number).