如何在JavaScript中以字符串形式表示的大整数中添加1? [英] How do I add 1 to a big integer represented as a string in JavaScript?
本文介绍了如何在JavaScript中以字符串形式表示的大整数中添加1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在JavaScript中为此字符串添加1?
How do I add 1 to this string in JavaScript?
var message = "12345612345678901234567890";
我希望输出如下:
"12345612345678901234567891"
我试过这个:
var message = "12345612345678901234567890";
message = parseInt(message);
var result = message + 1;
但 parseInt
以科学计数法返回一个值喜欢1.234567896453e + 25。
But parseInt
returned a value in scientific notation like 1.234567896453e+25.
推荐答案
尝试使用大整数库 BigInteger.js 添加大数字。
Try the big integer library BigInteger.js to add large numbers.
var message = "12345612345678901234567890";
var messageAsNumber = bigInt(message);
var messagePlusOne = messageAsNumber.add('1');
console.log(messagePlusOne.toString());
<script src="https://peterolson.github.io/BigInteger.js/BigInteger.min.js"></script>
这篇关于如何在JavaScript中以字符串形式表示的大整数中添加1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文