我一直在努力使我的结果在科学记数法中,仍然有无穷大的规则是非法的,但我似乎不能让它工作。当我使用toExponential()函数时,它会响应一条错误消息。请帮我个忙好吗?代码如下:
<!DOCTYPE html>
<html>
<body>
Base:<input id="n1" rows=1 cols=3 type=number">
Exponent:<input id="n2" rows=1 cols=3 type="number">
<script>
var calc=0
function calculate() {
calc=document.getElementById("result").innerHTML =
BigInt(document.getElementById("n1").value)**BigInt(document.getElementById("n2").value)
calc.toExponential()=document.getElementById("result");
}
</script>
<button onclick="calculate()">Calculate</button><p>=<span id="result" style=display:none></span>
</p>
<script src="decimal.js/decimal.js"></script>
</body>
</html>发布于 2021-05-01 13:12:46
BigInt上不存在toExponential。由于toExponential()存在于Number.prototype.toExponential()中,您需要将BingInt解析为浮点型,然后调用toExponential()。
function calculate() {
let calc = BigInt(document.getElementById("n1").value)**BigInt(document.getElementById("n2").value);
document.getElementById("result").innerHTML = parseFloat(calc).toExponential();
console.log(parseFloat(calc).toExponential());
}<!DOCTYPE html>
<html>
<body>
Base:<input id="n1" rows=1 cols=3 type=number">
Exponent:<input id="n2" rows=1 cols=3 type="number">
<button onclick="calculate()">Calculate</button><p>=<span id="result"></span>
</p>
</body>
</html>
https://stackoverflow.com/questions/67343013
复制相似问题