2

I am trying to parse a hex string to a decimal number in javascript, but i meet something so weird.

The hex string is '27a4b0795a7d749c', I know the decimal number is 2856602098915439772 checked by python and windows's calc. But the js's parseInt doesn't return right answer.

Here's the test code:

var hex = '27a4b0795a7d749c';
console.log(hex);
var num = parseInt(hex, 16);
console.log(num);
var hex2 = num.toString(16);
console.log(hex2);

the console display (I am using Chrome 30.0.1599.101 m on Windows 64bits):

result in chrome console

I test it in IE9, and it get 2856602098915439600 as well. But how does this happened?

Guangming Mao
  • 775
  • 1
  • 9
  • 15

1 Answers1

2

Per this answer (which cites ECMA specs) the largest Javascript integer is 9007199254740992 - way less than your expected value.

Community
  • 1
  • 1
Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
  • @lonesomeday points out a probably duplicate question, whose accepted answer goes into depth on how JS can have large, imprecise numbers. http://stackoverflow.com/questions/9297434/parseint-rounds-incorrectly O, JS! – Michael Paulukonis Nov 04 '13 at 15:30