How to convert string to float Number in Javascript

JavaScript parseFloat Function

While working with numbers and decimal point numbers in javascript, it is necessary to know what you can do with them, but the most important thing is how to do it.

In this Javascript tutorial, we will discuss about the javaScript parseFloat () function, you will learn what is parseFloat in javascript and what it does. You will learn from this javascript parseFloat example. After finishing this quick guide, you will be familiar with JavaScript Parsing () syntax and where you should use it.

Let’s learn more javascript parseFloat ()

Contents

  • About JavaScript parseFloat() Function
  • JavaScript parseFloat() Function Syntax
  • Example

About JavaScript parseFloat() Function

The javascript parseFloat () function returns an floating point number by parse the string. ParseFloat () Javascript has an inbuilt function.
ParseFloat is accept the string and convert it to a floating point number. If there is not put the value in this function, this is gives return NAN.

javaScript Replace() | javaScript String Replace All

JavaScript parseFloat() Function Syntax

The syntax of javascript parseFloat () is very simple. The JavaScript parseFloat () should look like this :

parseFloat(string)
  • Params : It accepts a parameter “string value” that contains a string that is changed to floating-point number.

To understand what parseFloat is in JavaScript, it is necessary to see an example. Take a look at this parseFloat function example – where we parse different numbers and strings :

Example

<script>
 
     // return float value
    a = parseFloat("  17  ")
    document.write('parseFloat("  17  ") = ' +a +"<br>");
    
    
    b = parseFloat("1234abcd")
    document.write('parseFloat("123abc") = '+b +"<br>");
    
    // returns NaN value
    c = parseFloat("abcd4567")
    document.write('parseFloat("abcd4567") = ' +c +"<br>");
    
    d = parseFloat("3.15")
    document.write('parseFloat("3.15") = '+d +"<br>");
    
    // returns only first Number
    e = parseFloat("23 12 2019")
    document.write('parseFloat("23 12 2019") = ' +e +"<br>");
 
</script>

Output

parseFloat(" 17 ") = 17
parseFloat("123abc") = 1234
parseFloat("abcd4567") = NaN
parseFloat("3.15") = 3.15
parseFloat("23 12 2019") = 23

JavaScript | string.localeCompare() - Examples

#javascript #programming

How to convert string to float Number in Javascript
65.75 GEEK