编码员您好,请告诉我哪里出了问题以及为什么 toFixed() 方法不起作用。我第一次学习javascript,所以我对它没有太多了解。请有人帮助我。
<!DOCTYPE html>
<html>
<body>
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
var x, y;
x = document.getElementById('numb').value;
document.getElementById('demo').innerHTML=x.toFixed(3);
}
</script>
</body>
</html>
//***Here toFixed() Method not working***
最佳答案
好吧,在使用 toFixed() 之前,您必须将字符串数字转换为浮点型
function myFunction() {
var x, y;
x = document.getElementById('numb').value;
document.getElementById('demo').innerHTML= parseFloat(x).toFixed(2);
}
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo">hui</p>
关于JavaScript 基本代码告诉我我有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59484932/