Rounding Numbers in Python

python_tutorials

Using a computer in order to do rather complex Math is one of the reasons this machine was originally developed. As long as integer numbers and additions, subtractions, and multiplications are exclusively involved in the calculations, everything is fine. As soon as floating point numbers or fractions, as well as divisions, come into play it enormously complicates the whole matter.

As a regular user, we are not fully aware of these issues that happen behind the scenes and may end up with rather surprising, and possibly inaccurate results for our calculations. As developers, we have to ensure that appropriate measures are taken into account in order to instruct the computer to work in the right way.

In our daily life we use the decimal system that is based on the number 10. The computer uses the binary system, which is base 2, and internally it stores and processes the values as a sequence of 1s and 0s. The values we work with have to be constantly transformed between the two representations. As explained in Python’s documentation:

…most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal

To finish reading, please visit source site