TIL: Quick rounding in JS
In the past I’d always used the
Math.round() function with floor and such to handle rounding to decimal places, turns out there’s a much simpler way of doing it…
To round to say 3 decimal places, just use the
toFixed() method that’s available to any
Number. For example:
The above will return
12.346, where the method automatically rounds up the value for you.
This is much shorter than the way I’d been doing the math in the past.