Problem
I get numbers from 1 to 5 including all possible floating point numbers in between. The output must contain two digits after comma and in case of after-comma digits they need to be rounded down (floor).
Example input and output:
- 1 -> 1.00
- 4.3 -> 4.30
- 1.1000 -> 1.10
- 1.5999 -> 1.59
My Try
My try is doing a Math.floor on the 100x of the number and dividing afterwards to get rid of the unwanted digits after comma. The Number.toFixed(2) gets me the possibly missing zeros afterwards:
(Math.floor(input * 100) / 100).toFixed(2)
The problem with this is JavaScript's floating point inprecision:
Math.floor(4.14 * 100) / 100
// results in 4.13 because 4.14 * 100 is 413.99999999999994