0

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.

Ben
  • 2,957
  • 2
  • 27
  • 55
  • Do you know the maximum number of decimal places you will have at any point? – Tushar Shahi Sep 03 '21 at 19:01
  • [Do any of the answers here suit your needs?](https://stackoverflow.com/questions/8522673/make-a-number-a-percentage) – Alexander Nied Sep 03 '21 at 19:02
  • @TusharShahi No I don't – Ben Sep 03 '21 at 19:02
  • @AlexanderNied No. I tried all the solutions there. The only thing that could work is .toFixed(2), but as I mentioned, I cannot tell how many decimal points the number will have, and I really don't want to start calculating it and making this into a serious script – Ben Sep 03 '21 at 19:04
  • I think it would be helpful to understand your requirements a bit better-- can you provide some expected inputs and expected outputs, and the criteria by which they are derived? Also, I see your question has been closed as a dupe-- I'd check the link and see if any of the answers there meet your needs-- if not, I'd suggest you update the question to explain what differentiates it from the duplicate post to make a case for reopening the post. – Alexander Nied Sep 03 '21 at 19:11
  • 1
    Why not just do .toFixed(15) for example, and then just remove '0' at right. It is not elegant but I don't see any other solution – AlexSp3 Sep 03 '21 at 19:12
  • @AlexanderNied Yeah, I checked the link. It is not helpful. I responded by editing my original question, but you are right. I will update again and show exactly what I need – Ben Sep 03 '21 at 19:12
  • 1
    @AlexandroPalacios this is actually not a bad idea :) Def add it as an answer since it does what I am looking for. Not nicely, but nicer than checking how many decimal points each number has and passing it to `.toFixed(NumOfDecimals)` – Ben Sep 03 '21 at 19:14
  • 3
    @Ben You can use 'parseFloat()' to get a number without extra '0', and then use 'String()'. It would be `String(parseFloat((0.0725 * 100).toFixed(15)))` – AlexSp3 Sep 03 '21 at 19:21
  • @AlexandroPalacios that's even better. Please add your suggestion as an answer and I'll mark it as such. Thank you! – Ben Sep 03 '21 at 19:25
  • 2
    Did you try [this answer](https://stackoverflow.com/a/45163573/215552) on the duplicate, without the `minimumFractionDigits` setting, but with a `maximumFractionDigits` of something large, like 20? – Heretic Monkey Sep 03 '21 at 19:31
  • 1
    @HereticMonkey Yes. That's it!! Thank you. – Ben Sep 03 '21 at 19:34

0 Answers0