Possible Duplicate:
Truncate leading zeros of a string in Javascript
What is the simplest and cross-browser compatible way to remove leading zeros from a number in Javascript ?
e.g. If I have a textbox value as 014 or 065, it should only return 14 or 65
We can use four methods for this conversion
10Update(based on comments): Why doesn’t this work on “large numbers”?
For the primitive type
Number, the safest max value is 253-1(Number.MAX_SAFE_INTEGER).Now, lets consider the number string ‘099999999999999999999’ and try to convert it using the above methods
All results will be incorrect.
That’s because, when converted, the numString value is greater than
Number.MAX_SAFE_INTEGER. i.e.,This means all operation performed with the assumption that the
stringcan be converted tonumbertype fails.For numbers greater than 253, primitive
BigInthas been added recently. Check browser compatibility ofBigInthere.The conversion code will be like this.
P.S: Why radix is important for
parseInt?If radix is undefined or 0 (or absent), JavaScript assumes the following:
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