1

I am trying to convert a string to number, which is "09"

parseInt('09') //returns 9 always

What is the best way to get the number with leading zero and it would be better if someone explain, why the designed parseInt in such a way that doesn't care about leading zeros.

Kaushik Thanki
  • 3,334
  • 3
  • 23
  • 50
divakar
  • 1,379
  • 6
  • 15
  • 31
  • 1
    parseInt() returns a number. Mathematically leading zeros are meaningless, so 9 = 09 = 009 = 9.0000, etc. If you want to retain leading zeros you need to keep it as a string. – nnnnnn Feb 06 '16 at 07:56
  • Simple, `09` equals `9` when considered as numbers. – Aycan Yaşıt Feb 06 '16 at 07:57
  • what other number do you expect to get? – terafor Feb 06 '16 at 07:57
  • 1
    Can you explain what you're trying to do? Maybe parseInt isn't what you're looking for, if it's an issue with padding, you can always format the number afterwards to have leading zeros. – Dave Chen Feb 06 '16 at 07:58
  • 1
    http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript – Sarfraz Feb 06 '16 at 08:03

2 Answers2

9

An int cannot have leading zeroes, since they don't have any mathematical meaning.

To store a number, languages use a binary representation (don't forget everything is 0s and 1s in the end). This representation is the same for 9, 09, or 00009. When the number must be printed, it is converted back to a string representation, so you lose the leading zeroes.

If you need to store/remember the 0s, your only choice is to store the string representation of the number.

What you could do is storing both the number and string representation, like this:

function MyInt(s){
    this.asString = s;
    this.num = parseInt(s);
}


var i = new MyInt("09");
console.log(i.num); // 9
console.log(i.asString); // 09

Take note that leading 0 don't have any value for int. If want to use for display purpose use string and for calculation part use parseInt(someval)

Kaushik Thanki
  • 3,334
  • 3
  • 23
  • 50
Derlin
  • 9,572
  • 2
  • 32
  • 53
  • Exactly, if you want to display with leading Zeros convert it to string and add the leading zeros at your controllers end. – Nasik Shafeek Feb 06 '16 at 08:19
1

parseInt takes two parameters: string and radix.

radix tells parseInt how to convert a string to a number.

If you leave the radix out, the rules are somewhat simple for how it decides what radix to use:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal).
    Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

  • If the input string begins with any other value, the radix is 10 (decimal).

That said, you are using a function whose non-abbreviated name is "Parse Integer." You cannot make it retain leading zeros, because leading zeros have no mathematical value.

rockerest
  • 10,412
  • 3
  • 37
  • 67