我只是試圖添加1到一個大數字,如果滿足一些條件。下面是我的javascript:如何將1添加到JavaScript中的大數字?
console.log('n1->'+n1+ ' - ' + typeof(n1));
console.log('n2->'+n2);
if(n1 != null && n2 != n1){
console.log('adding 1');
n2 = n1 + 1;
}
console.log('n2->'+n2);
,這裏是控制檯輸出:
n1->443751754287812600 - number
n2->null
adding 1
n2->443751754287812600
我期待有n2=443751754287812601
甚至n2=4437517542878126001
。你能解釋它爲什麼不起作用嗎?以及如何正確地完成總和?
感謝您的幫助。
閱讀:http://stackoverflow.com/questions/307179/what-is-javascripts -max-int-what-the-highest-integer-value-a-number-can-go-t – noobcode