# Round not rounding to upper number

Please do not put text into pictures. To avoid the Markdown formatting paste the text between triple backticks:

`````````
Your text will be here.
```
``````

It is not clear what is your question. What results do you expect and why?

A hint which can resolve your problem: Most of the decimal numbers cannot be represented exactly by `float`. Example:

``````>>> f'{5.805:.99}'
'5.80499999999999971578290569595992565155029296875'
``````

Another hint: Normally you do not need to round numbers. You round them just at the output - for example using the f-string formatting as shown above.

1 Like

Thank you i have to learn more about floating numbers

The cause of the inability to represent decimal numbers exactly is that `float` represents the numbers in binary (base-2 numeral system). It is explained in the Python documentation too:

https://docs.python.org/3/tutorial/floatingpoint.html

The important thing to remember is that floats are not the same as real numbers you learned about in school.

For instance, 1/3 as a real number goes on forever in decimal: 0.3333… and never stops. But 1/3 on a calculator might stop after (say) 8 or 10 digits, and as a float it ends with a bunch of non-3 digits:

``````print('%.60f' % (1/3))
# gives 0.333333333333333314829616256247390992939472198486328125000000
``````

The reason is that floats are actually binary (base 2) numbers, so the only numbers they can store exactly are made up of fractions of powers of two, everything else has to be rounded and is approximate:

``````0.5 = 1/2            # exact
0.125 = 1/8          # also exact
0.5625 = 1/2 + 1/16  # still exact
0.1 = 1/10           # 10 is not a power of 2, so APPROXIMATE
``````

That last one often surprises people. `0.1` as a float is actually:

``````print('%.60f' % 0.1)
# gives 0.100000000000000005551115123125782702118158340454101562500000
``````

If you use the `decimal` module instead, the numbers use decimal, not base 2, so they work better with numbers which are exact decimals:

``````from decimal import Decimal
Decimal("0.1")  # an exact decimal, so exact
Decimal(0.1)    # from an inexact binary float, so still inexact
Decimal(1)/10   # okay this is exact too
Decimal(1)/3    # rounded just like your calculator
``````

But:

• Decimals are slower and use more memory;
• they don’t work with all of the functions in the `math` module;
• they have the same kinds of rounding errors as floats;
• and for complicated reasons, calculations with Decimals have more and bigger rounding errors than binary floats.

So even though Decimal is less surprising for beginners, binary floats are what the professionals use for serious, high-powered numeric work.

3 Likes

Thank you so much this is the perfect answer 