# Rounding float numbers to two decimals

**URL:** <https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946>\
**Category:** CS Principles\
**Tags:** csp-unit-5\
**Created:** [January 10, 2019, 8:19pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946 "2019-01-10T20:19:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [January 10, 2019, 8:19pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/1 "2019-01-10T20:19:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bhatnagars](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bhatnagars/32/4919_2.png) [@bhatnagars](https://forum.code.org/u/bhatnagars)\
**Post date:** [January 10, 2019, 8:38pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/2 "2019-01-10T20:38:03Z")

</div>

try the toFixed function. Here is an example: [https://www.w3schools.com/jsref/jsref\_tofixed.asp](https://www.w3schools.com/jsref/jsref_tofixed.asp)

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [January 10, 2019, 8:44pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/3 "2019-01-10T20:44:22Z")

</div>

Thank you so much. I guess I was under the assumption that only [code.org](http://code.org)’s limited javascript commands would work on here. Thank you for showing me that other functions will work as well!

---

<div class="post-metadata">

**Author:** ![bhatnagars](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bhatnagars/32/4919_2.png) [@bhatnagars](https://forum.code.org/u/bhatnagars)\
**Post date:** [January 10, 2019, 8:49pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/4 "2019-01-10T20:49:01Z")

</div>

Not all functions work. Some do!

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mitch/32/2678_2.png) [@Mitch](https://forum.code.org/u/Mitch)\
**Post date:** [January 10, 2019, 8:51pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/5 "2019-01-10T20:51:12Z")

</div>

If you wanted to demonstrate an algorithm that lets you do this without resorting to introducing toFixed you can use this technique:

1. multiply the number by the power of ten equal to the number digits after the decimal that you want to keep
2. Then call round()
3. 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

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [January 10, 2019, 8:54pm UTC](https://forum.code.org/t/rounding-float-numbers-to-two-decimals/23946/6 "2019-01-10T20:54:58Z")

</div>

Actually I didn’t even think about that. But it makes perfect sense. Thanks!
