Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active February 7, 2016 17:23
Show Gist options
  • Save aloisdg/29e7884bd153967370e0 to your computer and use it in GitHub Desktop.
Save aloisdg/29e7884bd153967370e0 to your computer and use it in GitHub Desktop.
Why parseInt() cant print the good answer?

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("010­00000", 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("010­00000", 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment