当在字符串上下文中使用时,JavaScript将超过21位的整数转换为科学符号。我打印了一个整数作为URL的一部分。我怎样才能阻止这种转变的发生?
当前回答
如果您只是为了显示而这样做,您可以从四舍五入之前的数字构建一个数组。
var num = Math.pow(2, 100);
var reconstruct = [];
while(num > 0) {
reconstruct.unshift(num % 10);
num = Math.floor(num / 10);
}
console.log(reconstruct.join(''));
其他回答
有号码。固定,但如果数字>= 1e21,它使用科学计数法,最大精度为20。除此之外,你可以自己卷,但会很乱。
function toFixed(x) {
if (Math.abs(x) < 1.0) {
var e = parseInt(x.toString().split('e-')[1]);
if (e) {
x *= Math.pow(10,e-1);
x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
}
} else {
var e = parseInt(x.toString().split('+')[1]);
if (e > 20) {
e -= 20;
x /= Math.pow(10,e);
x += (new Array(e+1)).join('0');
}
}
return x;
}
上面使用了廉价-'n'-easy字符串重复((new Array(n+1)).join(str))。你可以用俄国农民乘法来定义String.prototype.repeat。
这个答案应该只适用于问题的上下文:显示一个大数字而不使用科学计数法。对于其他任何东西,您应该使用BigInt库,例如BigNumber、lemon 's BigInt或BigInteger。展望未来,新的原生BigInt(注意:不是lemon的)应该可用;Chromium和基于它的浏览器(Chrome、新的Edge [v79+]、Brave)和Firefox都支持;Safari的支持正在进行中。
BigInt(n).toString()
例子:
Const n = 13523563246234613317632; console.log("toFixed (wrong): " + n.toFixed()); console.log("BigInt(右):" + BigInt(n).toString());
Beware, though, that any integer you output as a JavaScript number (not a BigInt) that's more than 15-16 digits (specifically, greater than Number.MAX_SAFE_INTEGER + 1 [9,007,199,254,740,992]) may be be rounded, because JavaScript's number type (IEEE-754 double-precision floating point) can't precisely hold all integers beyond that point. As of Number.MAX_SAFE_INTEGER + 1 it's working in multiples of 2, so it can't hold odd numbers anymore (and similiarly, at 18,014,398,509,481,984 it starts working in multiples of 4, then 8, then 16, ...).
因此,如果你可以依赖BigInt支持,输出你的数字作为一个字符串传递给BigInt函数:
const n = BigInt("YourNumberHere");
例子:
const n1 = BigInt(18014398509481985);//错误,将四舍五入到18014398509481984 在' BigInt '看到它之前 console.log(n1.toString() + " <== error "); const n2 = BigInt("18014398509481985");//右,BigInt处理它 console.log(n2.toString() + " <== Right");
我知道这是一个老问题,但最近很活跃。MDN toLocaleString
const myNumb = 1000000000000000000000;
console.log( myNumb ); // 1e+21
console.log( myNumb.toLocaleString() ); // "1,000,000,000,000,000,000,000"
console.log( myNumb.toLocaleString('fullwide', {useGrouping:false}) ); // "1000000000000000000000"
您可以使用选项格式化输出。
注意:
Number.toLocaleString()四舍五入到小数点后16位,因此…
const myNumb = 586084736227728377283728272309128120398;
console.log( myNumb.toLocaleString('fullwide', { useGrouping: false }) );
返回…
586084736227728400000000000000000000000
如果准确性在预期结果中很重要,这可能是不可取的。
其他人的答案并不能给你确切的数字! 这个函数精确地计算所需的数字,并在字符串中返回它,以防止它被javascript更改! 如果你需要一个数值结果,只要乘以第一个函数的结果!
function toNonExponential(value) {
// if value is not a number try to convert it to number
if (typeof value !== "number") {
value = parseFloat(value);
// after convert, if value is not a number return empty string
if (isNaN(value)) {
return "";
}
}
var sign;
var e;
// if value is negative, save "-" in sign variable and calculate the absolute value
if (value < 0) {
sign = "-";
value = Math.abs(value);
}
else {
sign = "";
}
// if value is between 0 and 1
if (value < 1.0) {
// get e value
e = parseInt(value.toString().split('e-')[1]);
// if value is exponential convert it to non exponential
if (e) {
value *= Math.pow(10, e - 1);
value = '0.' + (new Array(e)).join('0') + value.toString().substring(2);
}
}
else {
// get e value
e = parseInt(value.toString().split('e+')[1]);
// if value is exponential convert it to non exponential
if (e) {
value /= Math.pow(10, e);
value += (new Array(e + 1)).join('0');
}
}
// if value has negative sign, add to it
return sign + value;
}
你可以使用number.toString(10.1):
console.log(Number.MAX_VALUE.toString(10.1));
注意:这目前适用于Chrome,但不适用于Firefox。规范规定基数必须是整数,所以这会导致不可靠的行为。
你的问题:
number :0x68656c6c6f206f72656f
display:4.9299704811152646e+23
你可以使用这个:https://github.com/MikeMcl/bignumber.js
用于任意精度的十进制和非十进制算术的JavaScript库。
是这样的:
let ten =new BigNumber('0x68656c6c6f206f72656f',16);
console.log(ten.toString(10));
display:492997048111526447310191