admin管理员组

文章数量:1356548

I have my code and it is working, but I'm trying to add the parseFloat option and 'toFixed()' to 2 decimal places. I'm confused on where these 2 lines of code should be placed. I created a unit conversion website that would convert the number of inches a user inputs and converts it to feet.

<!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>

I have my code and it is working, but I'm trying to add the parseFloat option and 'toFixed()' to 2 decimal places. I'm confused on where these 2 lines of code should be placed. I created a unit conversion website that would convert the number of inches a user inputs and converts it to feet.

<!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>
Share Improve this question asked Sep 16, 2018 at 16:49 MarkMark 311 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

you can add it in the feet() function where you get the value of the input and convert it to feet:

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

You can do in either of the ways

  1. While passing the value to function doOuput() in feet() or.
  2. in doOuput()

Working fiddle https://codepen.io/Ashish9342/pen/YOOawR

//On key up enter or pressing enter
function checkForEnter() {
  var input = document.getElementById("userinches");
  input.addEventListener("keyup", function(event) {
    event.preventDefault();
    //check if input has a greater than zero
    if (input.value.length > 0 && event.keyCode === 13) {
      document.getElementById("buttonft").click();
    }
  });
}
checkForEnter();



// converting the value to feet in 2 fixed decimal
function feet() {
  var userinches = document.getElementById('userinches').value;
  doOuput('The answer is ', userinches * 0.0833).toFixed(2);
  //doOuput('The answer is ', userinches * 0.0833);

}

function doOuput(val, unit) {
  document.getElementById('results').innerHTML = val + unit;
  //document.getElementById('results').innerHTML = val + unit.toFixed(2);
}

本文标签: javascriptParse Float and toFixed()Stack Overflow