;

How to Convert String to Number or Integer in javascript/Jquery


Tutorialsrack 02/01/2020 Jquery Javascript

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.

Convert String to Number By using Number() function

In my opinion, the simplest way would be to use the native Number function, this function takes care of the decimals as well:

Example - Convert String to Number By using Number() function
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.

Convert String To Number by Using parseInt() function

parseInt() method is used to convert string to Number (a whole number). 

This method accepts 2 arguments:

  1. 1st Parameter: string value to convert
  2. 2nd Parameter: Radix, it is a base number used in mathematical systems

Prefixes used in a String Value

  • No prefix - If there isn't a prefix, the radix is 10 (decimal).
  • 0 - If the value start at 0, then the radix is 8 (octal). Though, this feature is deprecated.
  • 0x - If the value starts at 0x, then the radix is 16 (hexadecimal).
Code - Convert String To Number by Using parseInt() function
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)

Convert String To Number by Using parseFloat() function

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.

Code - Convert String To Number by Using parseFloat() function
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)

Convert String To Number by Using Math.floor() function

Math.floor() function is used to convert String to Number but it returns only the integer part of the number.

Code - Convert String To Number by Using Math.floor() function
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

Convert String To Number by Using Math.ceil() function

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.

Code - Convert String To Number by Using Math.ceil() function
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

 

Convert String To Number by Using Unary Operator

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.

Code - Convert String To Number by Using Unary Operator
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

Convert String To Number by multiplying with 1(*1)

It is the fastest option and behaves like + unary operator.

Code - Convert String To Number by multiplying with 1(*1)
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

Convert String To Number by Adding Double tilde(~~) before String

Code - Convert String To Number by Adding Double tilde(~~) before String
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!

 


Related Posts



Comments

Recent Posts
Tags