admin管理员组文章数量:1287514
How do I add 1 to this string in JavaScript?
var message = "12345612345678901234567890";
I want the output like this:
"12345612345678901234567891"
I tried this:
var message = "12345612345678901234567890";
message = parseInt(message);
var result = message + 1;
But parseInt
returned a value in scientific notation like 1.234567896453e+25.
How do I add 1 to this string in JavaScript?
var message = "12345612345678901234567890";
I want the output like this:
"12345612345678901234567891"
I tried this:
var message = "12345612345678901234567890";
message = parseInt(message);
var result = message + 1;
But parseInt
returned a value in scientific notation like 1.234567896453e+25.
- You obviously need a third-party arbitrary precision library. Are you using an environment where you can install one, such as Node? – Álvaro González Commented Apr 25, 2017 at 15:05
-
1
parseInt(message) + 1
isn't it? – Sebastián Palma Commented Apr 25, 2017 at 15:06 - 4 @SebastiánPalma won't work, numbers are too big. MAX_SAFE_INTEGER in JavaScript is 9007199254740991. – Jared Smith Commented Apr 25, 2017 at 15:06
- 2 This question might help you, OP. – Yassine Badache Commented Apr 25, 2017 at 15:12
- 2 Possible duplicate of Large numbers to string in JavaScript – Jared Smith Commented Apr 25, 2017 at 15:13
3 Answers
Reset to default 8Try 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>
There is no need to use libraries (2022), you can just use JS BigInt object
let message = "12345612345678901234567890";
let messageBigInt = BigInt(message);
console.log(messageBigInt + BigInt(1)); // 12345612345678901234567891n
You can create an array from the string in .length
s of 3
beginning from the end of the string.
Use a pattern which checks if adding 1
would result in the index of the array as a number would sum to 1000
, if true
, increment previous array index by 1
and fill the current array index with "000"
.
The pattern below only checks and adjusts last two elements of array; the same pattern can be extended to check each index of array, to properly adjust one or more of the indexes to "000"
and increment the previous index by 1
.
let message1 = "12345612345678901234567890";
let message2 = "12345612345678901234567999";
let message3 = "12345612345678901234999999";
function addNumberToString(str, numToAdd, digits = []) {
const [N, len, max] = [3, str.length, 1000];
for (let i = -N, l = len; digits.length < len / N; i -= N, l -= N) {
digits.unshift(str.slice(i, l));
}
function add(m) {
if (+digits[digits.length - m] + numToAdd < max) {
let n = +digits[digits.length - m];
digits[digits.length - m] = String(Number(n + numToAdd));
} else {
const M = m + 1;
if (+digits[digits.length - M] + numToAdd < max) {
let n = +digits[digits.length - M];
digits[digits.length - M] = String(Number(n + numToAdd));
digits[digits.length - (M - 1)] = "0".repeat(N);
} else {
if (digits[digits.length - (m + 1)]) {
digits[digits.length - (M - 1)] = "0".repeat(N);
add(m + 1);
}
}
}
return digits.join("")
}
return add(1);
}
console.log(
addNumberToString(message1, 1)
, addNumberToString(message2, 1)
, addNumberToString(message3, 1)
);
本文标签: How do I add 1 to a big integer represented as a string in JavaScriptStack Overflow
版权声明:本文标题:How do I add 1 to a big integer represented as a string in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251002a2365784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论