我一直试图用科学记数法得出我的结果,但无穷大规则仍然是非法的,但我似乎无法让它发挥作用。当我使用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>
toExponential在BigInt上不存在。由于toExponential()
存在于Number.prototype.toExponential()
中,因此需要将BingInt解析为float,然后调用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>