Friday, 15 February 2013

number to string conversion error in javascript -



number to string conversion error in javascript -

did ever seek convert big number string in javascript?

please seek this:

var n = 10152557636804775; console.log(n); // outputs 10152557636804776

can help me understand why?

10152557636804775 higher maximum integer number can safely represented in javascript (it's number.max_safe_integer). see this post more details.

from mdn (emphasis mine):

the max_safe_integer constant has value of 9007199254740991. reasoning behind number javascript uses double-precision floating-point format numbers specified in ieee 754 , can safely represent numbers between -(2^53 - 1) , 2^53 - 1.

to check if given variable can safely represented integer (without representation errors) can utilize issafeinteger():

var n = 10152557636804775; console.assert(number.issafeinteger(n) == false);

javascript numbers tostring

No comments:

Post a Comment