如何返回一个大数而不将其转为指数形式?

问题描述 投票:0回答:2

在过去的一个小时里我一直在寻找如何让我的函数返回原始数字(不是指数形式)。我尝试使用

toLocaleString()
,但它也有限制。当数字太大时,
toLocaleString()
函数仅返回
1

function count(n) {
  let numbers = [];
  do {
    numbers.push(n);
    n--;
  } while (n > 0);
    const factorial = numbers.reduce((accumulator, currentValue) => accumulator * currentValue);

    return factorial.toLocaleString('fullwide', { useGrouping:false }).length;
};
javascript
2个回答
0
投票

您必须使用 BigInt 并在号码末尾附加“n”,例如 123123123234234234234234234234234n。现在您还可以从代码中删除 toLocaleString() 。

请参阅此链接以获取更多信息


0
投票

您可以考虑使用

BigInt
Number#toString()
:

function count(n) {
  let numbers = [];
  do {
    numbers.push(n);
    n--;
  } while (n > 0);
    const factorial = numbers.reduce((accumulator, currentValue) => accumulator * BigInt(currentValue), 1n);

    return factorial;
};

console.log(count(20).toString())

© www.soinside.com 2019 - 2024. All rights reserved.