I had students build a coin flipping simulation and at the end I wanted them to give the percentage of each option. However when it displays the number, it creates values like for example 53.25678900004. I would like to reduce that to only display two decimals. Math.round() will round to an integer and I would like better than that. Any ideas?
try the toFixed function. Here is an example: https://www.w3schools.com/jsref/jsref_tofixed.asp
Not all functions work. Some do!
If you wanted to demonstrate an algorithm that lets you do this without resorting to introducing toFixed you can use this technique:
- multiply the number by the power of ten equal to the number digits after the decimal that you want to keep
- Then call round()
- Then divide the number by the value that you used in step 1.
for example, to round to 2 decimal digits : Math.round(100 * 53.25678900004) / 100 results in 53.26
Actually I didn’t even think about that. But it makes perfect sense. Thanks!