Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert string "01" to number in JavaScript?

Tags:

javascript

I'd like to know if it is possible to use 2 digits in number in JavaScript if the number is less than 10?

for example

10,9,8,7,6 and so on to

10,09,08,07,06 as "number data type"

yes, it's possible to display the numbers in 2 digits when the numbers are less than 10 as a "string data type".

if(number < 10) {
    console.log(number) //number
    number = "0" + number;
    console.log(number) //string
}

but I want to use it as number data type so I can use

if(number == 01) {
    //some code here
}

is it possible?

like image 952
ha_ryu Avatar asked Jan 29 '16 10:01

ha_ryu


3 Answers

ParseInt Syntax

Reference

parseInt(string, radix);

Parameters

string The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.

radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Do not do

var number = "09";
if(number == 09) { // here it will not compare the type check the == and ===
    alert("OK: " + number)
} else {
    alert("PROBLEM: " + number);
}

correct Answer

var number = "09";
var decimal = parseInt(number,10);
if(decimal === 09) {
    alert("OK: " + decimal)
} else {
    alert("PROBLEM: " + decimal);
}

Check this in console

var first = 10 ;
var secn = "10";

first == secn // true because both are equal. 
first === secn // both are not equal by type(string and number)



var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

Number

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number is created using the Number() function.

Example

Number('123')     // 123
Number('12.3')    // 12.3
Number('12.00')   // 12
Number('123e-1')  // 12.3
Number('')        // 0
Number(null)      // 0
Number('0x11')    // 17
Number('0b11')    // 3
Number('0o11')    // 9
Number('foo')     // NaN
Number('100a')    // NaN
Number('-Infinity') //-Infinity

enter image description here

Image Reference

like image 52
Kaushik Avatar answered Oct 22 '22 09:10

Kaushik


It would work, but it wouldn't do what you want it to do. Numbers with leading zeros are literals for octal numbers (positional number system with the base of 8). So for example:

071 == 71
=> false

071 == 57
=> true

More here: http://www.w3schools.com/js/js_numbers.asp

like image 29
Adam Libuša Avatar answered Oct 22 '22 08:10

Adam Libuša


It works with parseInt. By default it's base 10. So even 09 is ok, but as @deceze pointed out it's better to explicitly use the radix 10:

var number = "09";
var decimal = parseInt(number, 10);
if(decimal == 09) {
    alert("OK: " + decimal)
} else {
    alert("PROBLEM: " + decimal);
}
like image 1
Gavriel Avatar answered Oct 22 '22 08:10

Gavriel