JavaScript | Convert String to Number

JavaScript | Convert String to Number

JavaScript | Convert String to Number.javascript convert string to number. In this tutorial, we will learn how to convert string to number in javascript. Learn various methods of javascript with examples to convert strings to numbers in javascript.

javascript convert string to number. In this tutorial, we will learn how to convert string to number in javascript. Learn various methods of javascript with examples to convert strings to numbers in javascript.

JavaScript Convert String to Number

This tutorial purpose is to explain and share the best ways to convert string into numbers in javascript.

1. First Method – parseInt()

You can use the javascript parseInt() method to convert the string to a number.

Note:- The javascript parseInt () function returns an integer number by parse the string. If the first character can not be changed in number, the function returns NaN.

Syntax:

parseInt(string, radix)

Ex:

// 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>");

Result 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 

2. Second Method – parseFloat()

You can use the javascript parseFloat() method, which is to convert the string into a floating number or a decimal number

Note:- The ParseFloat () Javascript has an inbuilt function. ParseFloat has accepted the string and convert it to a floating-point number.

Syntax:

parseFloat(string)

Ex:

// 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>");

Result of the above code is:

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

3. Third Method – Number()

You can also use the javascript number() method to convert the string into number in javascript.

Note:- If you want to pass string with a character or text, it can not be changed in number, the function returns NaN.

Syntax:

Number(string)

Ex:

// 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>");

Result of the above code is:

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

If you want to know more javascript string methods, you may like

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java vs. JavaScript: Know The Difference

Java vs. JavaScript: Know the Difference, Java vs. JavaScript: What's the Difference? Java vs. JavaScript: Major Similarities and Differences. pros and cons of JavaScript and Java.

Programming a Javascript Simon Game Tutorial

In this javascript tutorial, I recorded myself live programming an html5 javascript simon game.

Understanding Memoization And Dynamic Programming in Javascript

In this Javascript tutorial I will explain what memoization is, how to use it, when you should use memoization, how to use memoization, what dynamic programming is, how to use memoization in dynamic programming. Memoization is a big complicated word that you may have never even heard before, but you may be surprised to know that you are most likely already using memoization without even realizing it.