-3

I'm new to Javascript but am trying to create a script to take a diameter, display only 3 decimal places and round the third decimal place to the nearest 5. I found some code which I think will do what I need to do here but can't work out how to add it to my existing code. I've added the third field as a check to make sure the result has been "rounded" correctly, can anyone point me in the right direction?

jsfiddle

    function calculate() {
    var circBox = document.getElementById('circ').value;
    var diameterBox = document.getElementById('diameter');
    var diameterBox2 = document.getElementById('diameter2');
    var Diameter = circBox / 3.1416;
    var Diameter2 = Diameter.toFixed(5);
    var Diameter3 = Math.ceil(Diameter2 * 1000) / 1000;
    diameterBox.value = Diameter3;
    diameterBox2.value = Diameter2;
}
Community
  • 1
  • 1

1 Answers1

0

You can round to the nearest 0.005 value with

var Diameter3 = Math.round(Diameter2 * 200) / 200;
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70