During the development of a project, I faced a strange behavior.
When I try to execute a basic subtraction between two values yield by two parseInt()
, I get a wrong value.
Lets see:
console.log(parseInt("002B36", 16))
console.log(parseInt("01000000", 16))
console.log("and")
console.log("11062 - 16777216 = ")
console.log(11062 - 16777216)
console.log("but")
console.log(parseInt("002B36", 16) - parseInt("01000000", 16))
or
<!-- begin snippet: js hide: false -->
<!-- language: lang-js -->
var div = document.getElementById('fakeConsole');
div.innerHTML += parseInt("002B36", 16) + "<br />"; // 11062
div.innerHTML += parseInt("01000000", 16) + "<br />"; // 16777216
div.innerHTML += 11062 - 16777216 + "<br />";
div.innerHTML += parseInt("002B36", 16) - parseInt("01000000", 16);
<!-- language: lang-css -->
<!-- language: lang-html -->
<div id="fakeConsole">
</div>
<!-- end snippet -->
Any idea where it come from?
I did by writing this in Stack Overflow. You can find the answer by clicking here.