[英]toFixed not a function
试图在calc上显示数字,但它说它不是函数。 我之前使用过Number(variable.toFixed(x)),但这一次它似乎在document.getlementbyid内部不起作用。 有什么帮助吗?
if (vi === 0){
document.getElementById('result').innerHTML =
Number(reactant.toFixed(13));
} else if (vi >= 1 && tx === 0) {
document.getElementById('result').innerHTML =
Number(reactant2.toFixed(13));
} else if (vi >= 1 && tx > 0) {
document.getElementById('result').innerHTML =
Number(reactantspec.toFixed(13));
}
我不确定reactant
来自何处,但很有可能是字符串值而不是数字。
"11.12123".toFixed(2); <-- toFixed is not a function error (becuase "11.12" is a string)
11.12123.toFixed(2); <-- Success!
在运行toFixed
逻辑之前,您可能需要将字符串转换为数字:
Number("11.123").toFixed(2)
看下面的例子。 希望能帮助到你。
<!DOCTYPE html> <html> <body> <p>Click the button to display the fixed number.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var reactant = '5.5678935436453256434'; var n = Number(reactant).toFixed(13); document.getElementById("demo").innerHTML = n; } </script> </body> </html>
元素ID的返回值,即document.getElementById('result').innerHTML
是一个String,您应该使用parseInt()
将其转换为整数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.