This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function parseInt() doesn't parse numbers with leading 0 correctly
Strange issues when parsing in JS occur.
parseInt("08")
//The result is: 0
parseInt("07")
//The result is: 7
Why is this happening?
Because of the 0 prefix. It tells Javascript the number is Octal, in base-8. 8 isn't a legal octal digit.
Use parseInt("8") instead, or as #Gumbo so correctly pointed out - parseInt("08", 10)
Related
This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 1 year ago.
I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -
If there are two separate numbers in the string - return the first of them.
So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:
function count(apples) {
const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
console.log(number);
}
This question already has answers here:
Console.log output in javascript
(6 answers)
Closed 3 years ago.
console.log(01) results in 1
But
console.log(011) results in 9
Can someone explain how console.log works with such numbers?
It's not about console.log, a number that starts with 0 is octal notation
console.log(+"011") // if you use like this it will work
011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.
This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 7 years ago.
I have this code:
console.log(066); // 54
Why does it log 54, not 66?
In JavaScript, numeric literals that begin with a 0 are treated as octal.
From the MDN docs:
Octal number syntax uses a leading zero. If the digits after the 0 are outside the range 0 through 7, the number will be interpreted as a decimal number.
Because add a prefix 0 will make the number to be considered of base 8(octal), as way 0x will make the following number to be of base 16(hexa)
This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?
Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: why does parseInt(1/0, 19) return 18?
Why does parseInt(1/0, 19) evaluate to 18 in Javascript ? I understand 19 in not a permissible radix but still can someone tell how things are working here ?
Ah, quick javascript consoling led to the answer:
> 1/0
Infinity
> parseInt("Infinity", 19)
18
parseInt seems to convert the first argument to a string, e.g.:
> parseInt(11, 2)
3
so, it's converting the string "Infinity", which explains everything.