0

I'm trying to display values on website using

let chance = Math.pow(0.2, vict)*100;

The output gives dynamic value, and I need 0.16000000000000003% to show as 0.16% and 0.00025600000000000015% as 0.000256% and so on. How can I round it, so I'm not limited to fixed decimal number.

Edit: vict is a whole number at all times. I just need to remove the error as @Caltrop has stated in comments. chance should display the probability. I cannot use suggested option with .toFixed, because as I said, I don't want to be limited to fixed decimal number in dynamic value.

8
  • @benbotto That question does not seem related to this one. The user is asking how to display floats without accumulated error, which is not equivalent to rounding to a fixed number of decimal places. Commented Jul 13, 2022 at 14:41
  • Have you thought of using Decimal.js to avoid floating-point round-off errors? Commented Jul 13, 2022 at 14:41
  • Number(0.16000000000000003).toFixed(2) outputs 0.16. No need for decimal.js yet, though I have used it too, it's pretty cool. Commented Jul 13, 2022 at 14:43
  • What if the number went on like this: 0.02123917198656.. where should the cut-off point be? It is easy to cut off zeros at the end, but other numbers exist as well. Please update the question what should happen with those. Commented Jul 13, 2022 at 14:48
  • 1
    I guess this won't be reopened soon. If you know what I mean, use a regular expression to remove the later part of the number: chance.toString().replace(/^(\d+\.0*[1-9]*).+/, '$1') Commented Jul 14, 2022 at 12:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.