Progrider

Programming discussion

Board index
Help · ✓ Resolved

Why does my grade average come out as 79.99999999

Adding up marks that have decimals and then the total is sometimes 79.99999999 instead of 80. Is my computer broken? The numbers are like 12.1 and 7.3.

3 replies

No, it's how floats work. Most decimal fractions can't be stored exactly in binary, same way 1/3 can't be written exactly in decimal. Try 0.1 + 0.2 in Python. For display use round(x, 2) or a format string like f"{x:.1f}".
For grades, that's fine. If it's money, don't use floats at all. Keep integer cents or use decimal.Decimal, and construct it from strings: Decimal("0.1"). Decimal(0.1) has the float's error in it already.
So it's fine as long as I round when showing it. That's a relief. thanks!

Discussion resolved

This discussion is resolved. Its creator can reopen it to accept more replies.