The Easy Way
Imagine we fetch the number below from an API - first- or third-party, it is not important in this case.
In this case we have gotten the number above, which has six (6) decimals, but as we very much love to better the user-experience (UX), and actually make the number more understandable, we would like to floor to four (4) decimals instead.
Yes, it is actually that easy! Although, sometimes we don't care if the number
does up or down, thus using the
Math.round()-function is quite handy
as well. We could simply switch the floor for our deloved
round, alas, sadly with some numbers, we end up rounding wrong this is
not something we will go into details with, but you can read more
here if you
The TL;DR is that you can simply add Epsilon to the original number, before we do anything else with it. The snippet is written as below.