0

I have a incremental control in the UI that increase/decrease values by clicking left and right arrows. See the UI Image:

enter image description here

I'm trying to round a float returned by UI to two decimal points. It works most of the times but I get raw values once in a while. Here is the sample code"

input = document.getElementById("in");
input.addEventListener("input", function() {
    console.log(input.value + " rounded : " + Math.round((input.value  - 0.01) * 100) / 100);
});
<input id="in" type="number"></input>

Any thoughts?

10 Rep
  • 2,217
  • 7
  • 19
  • 33

1 Answers1

0

that's just how it goes with floats and computers, sometimes it just slightly misses.

use .toFixed(2) to round to 2 decimal places.

Matthew S.
  • 43
  • 5