In this article, we will learn how to convert string to number or integer using javascript/jquery. To convert a string to a number or integer, javascript provides various functions such as Number()
, parseInt()
, parseFloat()
, Math.floor()
, Math.ceil()
, Unary Operator(+)
or Multiply by 1
which takes a string as input and returns an integer or number. In another article, you can learn how to convert number to string in javascript or jquery.
In my opinion, the simplest way would be to use the native Number function, this function takes care of the decimals as well:
var a = Number('1000'); //it will return 1000
var b = Number('10,000') //it will return NaN (Not a Number)
var c = Number('10.00') //it will return 10
var d = Number('10.51') //it will return 10.51
// if string value contains any character other than the number, then it return NaN (Not a Number)
var e = Number('10 hello') //it will return NaN (Not a Number)
var f = Number('hello 10') //it will return NaN (Not a Number)
If Number() function doesn't work for you, then you can use other methods such as parseInt()
, parseFloat()
, Math.floor()
, Math.ceil()
, Unary Operator(+)
or Multiply by 1
.
parseInt()
method is used to convert string to Number (a whole number).
This method accepts 2 arguments:
Prefixes used in a String Value
var a = parseInt('1000') // it will return 1000
var b = parseInt('1000.51') // it will return 1000
var c = parseInt('1000,00') // it will return 1000
var d = parseInt('1000 hello') // it will return 1000
//if string does not start with a number, then you will get a NaN(Not a Number)
var e = parseInt('hello 1000') // it will return NaN (Not a Number)
parseFloat()
function is used to convert String to Number(with decimal points). If string value contains a number with decimal points, then this function will retain those decimal points. This function takes only one parameter.
var a = parseFloat('253'); // it will return 253
var b = parseFloat('253.53'); // it will return 253.53
var c = parseFloat('253.25hello'); // it will return 253.25
//if string does not start with a number, then you will get a NaN(Not a Number)
var d = parseFloat('hello 253'); // it will return NaN (Not a Number)
Math.floor()
function is used to convert String to Number but it returns only the integer part of the number.
var a = Math.floor('10,000') //it will return NaN
var b = Math.floor('10.000') //it will return 10
var c = Math.floor('10.00') //it will return 10
var d = Math.floor('10.20') //it will return 10
var e = Math.floor('10.81') //it will return 10
var f = Math.floor('100') //it will return 100
var g = Math.floor('100 hello') //it will return NaN
This function is very similar to Math.floor()
function. Math.ceil()
function always rounds a number up to the next largest whole number or integer.
var a = Math.ceil('10,000') //it will return NaN
var b = Math.ceil('10.000') //it will return 10
var c = Math.ceil('10.00') //it will return 10
var d = Math.ceil('10.20') //it will return 11
var e = Math.ceil('10.81') //it will return 11
var f = Math.ceil('100') //it will return 100
var g = Math.ceil('100 hello') //it will return NaN
Unary Operator: Operators that operate on one operand are known as unary operators. Here we use the Plus (+) sign before the string value and it is the fastest way.
var a = +'10,000' //it will return NaN
var b = +'10.000' //it will return 10
var c = +'10.00' //it will return 10
var d = +'10.20' //it will return 10.2
var e = +'10.81' //it will return 10.81
var f = +'100' //it will return 100
var g = +'100 hello' //it will return NaN
It is the fastest option and behaves like + unary operator.
var a = '10,000' * 1 //it will return NaN
var b = '10.000' * 1 //it will return 10
var c = '10.00' * 1 //it will return 10
var d = '10.20' * 1 //it will return 10.2
var e = '10.81' * 1 //it will return 10.81
var f = '100' * 1 //it will return 100
var g = '100 hello' * 1 //it will return NaN
var a = ~~'10,000' //it will return 0
var b = ~~'10.000' //it will return 10
var c = ~~'10.00' //it will return 10
var d = ~~'10.20' //it will return 10
var e = ~~'10.81' //it will return 10
var f = ~~'100' //it will return 100
var g = ~~'100 hello' //it will return 0
I hope this article will help you to understand how to convert string to number or integer using javascript/jquery.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments