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.