解析浮动和toFixed()

时间:2018-09-16 16:49:17

标签: javascript html css parsefloat tofixed

我有我的代码,它正在运行,但是我试图将parseFloat选项和'toFixed()'添加到2个小数位。我对这两行代码应该放在哪里感到困惑。我创建了一个单位换算网站,该网站将转换用户输入的英寸数并将其转换为英尺。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <center><title>Unit Coversion Table</title><center>
</head>
<body>

<div id="question">
  <h2>Unit Convertor</h2>
  <p>Enter a Quanity of Inches: <input id="userinches" type="number" /> </p>
</div>  <!-- End of question div section -->

<div id="conversion">
  <script>
    function feet() {
      var userinches = document.getElementById('userinches').value;
      doOuput('The answer is ', userinches * 0.0833);
    }

    function doOuput(val, unit) {
      document.getElementById('results').innerHTML = val + unit;
    }
  </script>
</div>  <!-- End of conversion div section -->

<div="buttons">
  <button type="button" id="buttonft" onclick="feet();">Feet</button>
</div> <!-- End of buttons div section -->

<div id="results"></div>  <!-- End of results div section -->

</body>
</html>

2 个答案:

答案 0 :(得分:4)

您可以将其添加到foots()函数中,在该函数中您可以获取输入值并将其转换为foots:

function feet() {
  var userinches = document.getElementById('userinches').value;
  var feetValue = parseFloat(userinches * 0.0833).toFixed(2);
  doOuput('The answer is ', feetValue);
}

答案 1 :(得分:0)

您可以通过以下两种方式之一进行操作

  1. 在将值传递到foot()或。中的doOuput()函数时。
  2. 在doOuput()

工作小提琴https://codepen.io/Ashish9342/pen/YOOawR

 <input name="Employees[0]" value=Yes type="radio">
 <input name="Employees[0]" value=No type="radio">

 <input name="Employees[1]" value=Yes type="radio">
 <input name="Employees[1]" value=No type="radio">

 <input name="Employees[2]" value=Yes type="radio">
 <input name="Employees[2]" value=No type="radio">

 <input name="Employees[3]" value=Yes type="radio">
 <input name="Employees[3]" value=No type="radio">