【发布时间】:2023-11-02 14:26:01
【问题描述】:
我想在网站上显示一些现在采用科学计数法的数字。我正在使用 toPrecision 来显示数字的正常表示法。
不幸的是 toPrecision 只能在 1e-6 到 1e20 的范围内工作,而我确实有像 1e-7 和 1e-10 这样的数字。
那么当 toPrecision 不能完成我希望它完成的工作时,我该怎么办?
我尝试使用 Number() 和 parseFloat() 甚至两者都尝试让这个数字以正常表示法显示...
var min = 1e7,
nr1 = parseFloat(Number(min).toPrecision()),
nr2 = Number(min).toPrecision(),
nr3 = min.toPrecision(),
nr4 = min.toString();
console.log(nr1); //1e-7
console.log(nr2); //1e-7
console.log(nr3); //1e-7
console.log(nr4); //1e-7
到目前为止没有任何效果。
任何帮助将不胜感激
【问题讨论】:
-
你试过
toExponential吗? -
toExponential 会将正常的符号转换为科学的。结果是一样的。
-
你试过
var min = 1e42; min.toLocaleString();吗?这会将科学记数法转换为十进制记数法。 -
我尝试使用 toLocaleString();没用。我创建了一个小函数来使用 toFixed 解决这个问题,它能够工作。
标签: javascript scientific-notation