JavaScript Convert String to Number

Convert string to number in JavaScript; Through this tutorial, i am going to share with you how to convert string to number or decimal number in javaScript.

JavaScript Convert String to Number

Use the following 3 different ways or methods to convert string to number or 2,3,4, etc, decimal places in javascript; as shown below:

  • Convert string to int in javascript with parseInt()
  • Convert string to int in javascript with parseFloat()
  • Convert string to int in javascript with number()

Convert string to int in javascript with parseint

Using the javascript parseInt() method to convert the string to a number.

Syntax of parseInt() function

parseInt(string, radix)

Example 1 – Convert string to integer in javascript using parseInt

Here, i will take this simple example of convert string to number in JavaScript using parseInt() function; as shown below:

    // returns a Integer no
    a = parseInt("20");
    document.write('parseInt("20") = ' + a + "<br>");
    //  Not a Number character
    b = parseInt("30.00");
    document.write('parseInt("30.00") = ' + b + "<br>");
    // returns NaN on Non numeral character
    c = parseInt("2.366");
    document.write('parseInt("2.366") = ' + c + "<br>");
    // returns Integer value of a Floating point Number
    d = parseInt("3.14");
    document.write('parseInt("3.14") = ' + d + "<br>");
    // only first Number it encounters
    e = parseInt("22 3 2019");
    document.write('parseInt("22 3 2019") = ' + e + "<br>");

Output of the above code is:-

parseInt("20") = 20
parseInt("30.00") = 30
parseInt("2.366") = 2
parseInt("3.14") = 3
parseInt("22 3 2019") = 22 

Convert string to int in javascript with parseFloat()

Using javascript parseFloat() method to convert the string into a floating number or 2 , 3 etc decimal places.

Syntax of javaScript parseFloat() method

parseFloat(string)

Example 1 – Convert string to int in javascript with parseFloat()

Here, i will take example for convert string to number with 2,3, 4,etc decimal places in aJavaScript using parseFloat() method; as shown below:

 // return float value
a = parseFloat("  10  ")
document.write('parseFloat("  10  ") = ' +a +"<br>");
b = parseFloat("123abc")
document.write('parseFloat("123abc") = '+b +"<br>");
// returns NaN value
c = parseFloat("abc456")
document.write('parseFloat("abc456") = ' +c +"<br>");
d = parseFloat("3.14")
document.write('parseFloat("3.14") = '+d +"<br>");
// returns only first Number
e = parseFloat("18 2 2019")
document.write('parseFloat("18 2 2019") = ' +e +"<br>");

Output of the above code is:

parseFloat(" 10 ") = 10
parseFloat("123abc") = 123
parseFloat("abc456") = NaN
parseFloat("3.14") = 3.14
parseFloat("18 2 2019") = 18

Convert string to int in javascript with number()

Using javascript number() method to convert the string into number in javascript.

Syntax of JS number() method

Number(string)

Example 1 – Convert string to int in javascript with number()

Here, i will take an example for convert string to integer in javascript using number() method; as shown below:

// return float value
a = Number("  10  ")
document.write('Number("  10  ") = ' +a +"<br>");
b = Number("125.3")
document.write('Number("125.3") = '+b +"<br>");
// returns NaN value
c = Number("abc456")
document.write('Number("abc456") = ' +c +"<br>");
d = Number("42px")
document.write('Number("42px") = '+d +"<br>");

Output of the above code is:

Number(” 10 “) = 10
Number(“125.3”) = 125.3
Number(“abc456”) = NaN
Number(“42px”) = NaN

More JavaScript Tutorials

Leave a Comment