Javascript - 获取非常大的数字作为包含所有数字的字符串
Javascript - get very big number as string containing all digits
我已经为 Number.prototype.toFixed
查阅了 mozilla developer reference,这让我相信如果我有一个非常大的科学记数法显示的数字,我可以这样做来查看整个数字:
myBigNum.toFixed(2);
不幸的是,这不起作用。比如我想看Number.MAX_VALUE
的全部308个光荣数字,下面还是给我显示一个科学计数的字符串:
console.log(Number.MAX_VALUE.toFixed(2));
console.log(Number.MAX_VALUE.toFixed(2).constructor.name);
如何获得显示 javascript 中任意非常大(或非常小)数字的所有数字的字符串?
(注意:我意识到许多尾随数字可能表现出浮点数!)
您可以使用 toLocaleString
方法(如果需要,可以使用一些选项):
const s = Number.MAX_VALUE.toLocaleString(undefined, {
style: 'decimal',
useGrouping: false //Flip to true if you want to include commas
});
console.log(s)
我已经为 Number.prototype.toFixed
查阅了 mozilla developer reference,这让我相信如果我有一个非常大的科学记数法显示的数字,我可以这样做来查看整个数字:
myBigNum.toFixed(2);
不幸的是,这不起作用。比如我想看Number.MAX_VALUE
的全部308个光荣数字,下面还是给我显示一个科学计数的字符串:
console.log(Number.MAX_VALUE.toFixed(2));
console.log(Number.MAX_VALUE.toFixed(2).constructor.name);
如何获得显示 javascript 中任意非常大(或非常小)数字的所有数字的字符串?
(注意:我意识到许多尾随数字可能表现出浮点数!)
您可以使用 toLocaleString
方法(如果需要,可以使用一些选项):
const s = Number.MAX_VALUE.toLocaleString(undefined, {
style: 'decimal',
useGrouping: false //Flip to true if you want to include commas
});
console.log(s)