A moderator marked the question as duplicate of this question. It is not. Notice that this reference is also answered with solutions that assume two decimal places.
I am looking for a simple way to convert an array of numbers to array of percentages but only show the decimal places in the original number. For example:
I want 0.0725 to be 7.25%
I want 0.1 to be 10%
I want 0.052 to be 5.2%
I want 0.08254 to be 8.254%
etc.
Regarding .toFixed()
:
0.0725 * 100 = 7.249999999999999
(0.0725 * 100).toFixed(3) = 7.250
.toFixed
is almost a good solution, but I'm converting a bunch of number, like 0.01 and 0.082. So I can't tell in advance how many decimal places the number has
Math.floor
and Math.round
are also not helpful.
This simple requirement is ridiculously difficult to achieve
Oh, and I don't want to load something like lodash or underscore just to get this working
Any recommendations?
I apologize if this was previously answered. I could not find a suitable solution anywhere.