-1

I am trying to create a simple app where any inputted number is rounded to the nearest tenth. A user inputs a number into the field, clicks submit, and it outputs the rounded number.

For example, 5.678 is inputted, 5.7 is returned.

Any help is appreciated.

<!DOCTYPE html>
<html>
<body>


<input type="number" id="b6" placeholder="Liters" name="name" maxlength="100">

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {

    var m = document.getElementById("b6").value;
    var n = m.toFixed(1);

    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>
Phil Motto
  • 131
  • 2
  • 13
  • If you are open to libraries... Try accountingJS http://openexchangerates.github.io/accounting.js/ – jremi Aug 10 '18 at 14:09
  • `value` is always a string; see [the second dupetarget](http://stackoverflow.com/questions/32945779/incorrect-result-in-javascript-calculation/32945816#32945816). Once you convert to number, [the first dupetarget](https://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript) helps you round it. – T.J. Crowder Aug 10 '18 at 14:10

1 Answers1

0

var m is probably a string, so you may want to do var n = parseFloat(m).toFixed(1);

Luka Kostic
  • 314
  • 3
  • 12