I have some zeros prior to a positive integer. I want to remove the zeros so only the positive integer remains. Like '001' will only be '1'. I thought the easiest way was to use parseInt('001'). But what I discovered is that it don't works for the number 8 and 9. Example parseInt('008') will result in '0' instead of '8'.
Here are the whole html code:
<html> <body>
<script>
var integer = parseInt('002');
document.write(integer);
</script>
</body> </html>
But can I somehow report this problem? Do anyone know an another easy workaround this problem?
The parseInt function parses a string argument and returns a number with the leading zeros removed.
The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .
parseInt() doesn't always correctly convert to integer In JavaScript, all numbers are floating point. Integers are floating point numbers without a fraction. Converting a number n to an integer means finding the integer that is “closest” to n (where “closest” is a matter of definition).
parseFloat( ) parseFloat() is quite similar to parseInt() , with two main differences. First, unlike parseInt() , parseFloat() does not take a radix as an argument. This means that string must represent a floating-point number in decimal form (radix 10), not octal (radix 8) or hexadecimal (radix 6).
The range of int is between Integer. MAX_VALUE and Integer. MIN_VALUE inclusive (i.e. from 2147483647 down to -2147483648). You cannot parse the int value of out that range.
You have to specify the base of the number (radix)
parseInt('01', 10);
This is documented behavior: http://www.w3schools.com/jsref/jsref_parseInt.asp
Strings with a leading '0' are parsed as if they were octal.
Number prefixed with zero is parsed as octal.
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