0

I have a simple calculation, a number between 19 and 55 is in the Input and the Output shows the calculation. I cannot figure out how to control the number of decimal points. Ideally, I would like to have 1 or 2 decimal places.

Here is the code I have so far:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8"> 
  <title>Output Formatting</title>
  <script src="https://d3js.org/d3-format.v1.min.js"></script>
</head>
<body>
  <form oninput="result.value=(-1.0797 * parseInt(a.value) * parseInt(a.value)) + (52.466 * parseInt(a.value) - 820.183)">
    <input id="aa" type="number" name="a" onchange="setTwoNumberDecimal" min="19" max="55" step="0.25" value="22.00" /> <br>
    <output id="polar" name="result">-</output>
  </form>
</body>
</html>

I have seen some examples of D3.js being applied to control formatting, but I cannot figure out how to implement it here. The function doesn't appear to run, but I am not getting console errors:

<script>
function setTwoNumberDecimal(){
var format = d3.format(".3n");
var fname = d3.format(".3n")(document.getElementById('aa').value);
document.getElementById('polar').innerHTML = fname;
}
</script>

I also have a JS_Fiddle

fonsi
  • 471
  • 8
  • 22
  • 2
    use [Math.round](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) or [number.prototype.toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) – Adam H Oct 10 '18 at 21:08
  • 5
    You should very much look into removing the JS logic from your HTML. It makes it a nightmare to read and debug. Also, you're most likely looking for `.toFixed()` – Sterling Archer Oct 10 '18 at 21:09
  • Thanks @adam-h ! Math.round() solved the issue. And yes I need to improve my skills to separate HTML and JS. I am very new to JS. – fonsi Oct 10 '18 at 21:18

1 Answers1

-1
<form oninput="var n=(-1.0797 * parseInt(a.value) * parseInt(a.value)) + (52.466 * 
parseInt(a.value) - 820.183); result.value = n.toFixed(2)">
Muttley
  • 502
  • 2
  • 10