-2

Many thanks to all who helped me on the functions before- my issue now is i would like the result to round up to only two places after the decimal. I seen some answers but do not understand how to apply them. Thanks to help out a newb!

Example of my results 5.958480999999999

Example of the working functions

   function wireOd(valNum) {
      document.getElementById("myResult").innerHTML = valNum * valNum * .7854;

    }

    function cal() {
      var numZero5 = document.getElementById('num05').value;
      var numZero6 = document.getElementById('num06').value;
      var numZero7 = document.getElementById('num07').value;
      var total3 = parseFloat(numZero5) * parseFloat(numZero6) * parseFloat(numZero7) * .7854 * 1.5;
      var p = document.getElementById('total3');
      p.innerHTML = total3;
    }
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
SkidFx
  • 7
  • 2
  • This appears to be a duplicate: https://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places or https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary or https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places – benbotto Jan 09 '20 at 22:06
  • It isn't clear whether this question needs `round` or `toFixed`, but it's a duplicate of something either way. – Brilliand Jan 09 '20 at 22:07
  • Here is the html if it helps-

    Stem Weight Calculation

    Pounds Per Foot:

    – SkidFx Jan 09 '20 at 22:11

1 Answers1

1

For example you can use Number.prototype.toFixed() in order to achieve the goal, per the docs:

The toFixed() method formats a number using fixed-point notation.

Like the following:

const result = 5.95848099999999;
console.log(result.toFixed(2));

I hope that helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • Thank you, but if the result is different every time how can i write the script? The result i gave will never be the same value- dependant on the user input values. – SkidFx Jan 09 '20 at 22:14
  • I think you have the line `var total3 = parseFloat(numZero5) * parseFloat(numZero6) * parseFloat(numZero7) * .7854 * 1.5;` where you define the value for representation, so in the next line, you could use something like: `p.innerHTML = total3.toFixed(2);`. I hope that helps! – norbitrial Jan 09 '20 at 22:16
  • @norbitrial- that worked great thank you for the expertise- i have one last question my other function - how would i get the same result in this function? - function wireOd(valNum) { document.getElementById("myResult").innerHTML = valNum * valNum * .7854; } – SkidFx Jan 10 '20 at 14:56
  • Just simply use `document.getElementById("myResult").innerHTML = (valNum * valNum * .7854).toFixed(2);` in the function. Or store in a variable and call the `toFixed()` function on that. – norbitrial Jan 10 '20 at 16:34