How to parseInt "09" into 9 ?
Use parseInt() to remove leading zeros from a number in JavaScript.
The format() method of String class in Java 5 is the first choice. You just need to add "%03d" to add 3 leading zeros in an Integer. Formatting instruction to String starts with "%" and 0 is the character which is used in padding. By default left padding is used, 3 is the size and d is used to print integers.
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String. The ^0+(?! $)"; To remove the leading zeros from a string pass this as first parameter and “” as second parameter.
include the radix:
parseInt("09", 10);
This has been driving me nuts -parseInt("02")
works but not parseInt("09")
.
As others have said, the solution is to specify base 10:
parseInt("09", 10);
There's a good explanation for this behaviour here
... In Javascript numbers starting with zero are considered octal and there's no 08 or 09 in octal, hence the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With