How to convert string into float in JavaScript?

We can convert a string into a float in JavaScript by using some methods which are described below:

Method 1: In this method we will use the Type Conversion feature of JavaScript which will convert the string value into float.

Example: Below program demonstrates the above approach



filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
    // Javascript script  
    // to convert string
    // to float value 
    
    // Function to convert 
    // string to float value
    function convert_to_float(a) {
          
        // Type conversion
        // of string to float
        var floatValue = +(a);
          
        // Return float value
        return floatValue; 
    
    
//Driver code 
var n = "55.225";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
  
var n = "-33.565";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
</script> 

chevron_right


Output:

Converted value = 55.225
Type of 55.225 = number
Converted value = -33.565
Type of -33.565 = number

Method 2: In this method we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.

Example: Below program demonstrates the above approach

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
    // Javascript script  
    // to convert string
    // to float value 
    
    // Function to convert 
    // string to float value
    function convert_to_float(a) {
          
        // Using parseFloat() method
        var floatValue = parseFloat(a);
          
        // Return float value
        return floatValue; 
    
    
//Driver code 
var n = "245.165";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
  
var n = "-915.55";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
</script> 

chevron_right


Output:

Converted value = 245.165
Type of 245.165 = number
Converted value = -915.55
Type of -915.55 = number

Special Case: In French the float numbers are written by the use of comma (, ) as separator instead of dot(.) as separator.
Example:

The value 245.67 in French is written as 245, 67

To convert French string into float in JavaScript we will first use replace() method to replace every (, ) with (.) then follow any of above described method.

Example: Below program demonstrates the above approach

filter_none

edit
close

play_arrow

link
brightness_4
code

<script> 
    // Javascript script  
    // to convert string
    // to float value 
    
    // Function to convert 
    // string to float value
    function convert_to_float(a) {
          
        // Using parseFloat() method
        // and using replace() method
        // to replace ', ' with '.'
        var floatValue = parseFloat(a.replace(/, /, '.'));
          
        // Return float value
        return floatValue; 
    
    
//Driver code 
var n = "245, 165";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
  
var n = "-915, 55";
    
// Call function 
n = convert_to_float(n); 
    
// Print result 
document.write("Converted value = "
        n + "</br> Type of " + n + " = " 
        +typeof n + "<br>");
</script> 

chevron_right


Output:

Converted value = 245.165
Type of 245.165 = number
Converted value = -915.55
Type of -915.55 = number


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.