This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: round displays 2 instead of 3 digits
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kenfos, steven.daprano
Priority: normal Keywords:

Created on 2021-09-22 09:51 by kenfos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg402413 - (view) Author: Kenneth Fossen (kenfos) Date: 2021-09-22 09:51
When round is given 3 as argument for number of decimal points, the expected behaviour is to return a digit with 3 decimal points

Example:

ig1 = 0.4199730940219749
ig2 = 0.4189730940219749
print(round(ig1, 3)) # 0.42 expected  to be 0.420
print(round(ig2, 3)) # 0.419
msg402418 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-09-22 10:24
"the expected behaviour is to return a digit with 3 decimal points"

I presume you mean a float with 3 digits after the decimal point. Three decimal points would be a bug :-)

I am afraid that you are misinterpreting what you are seeing. Rounding a number does not force it to display with trailing zeroes. Floats display using the most natural form, which means trailing zeroes are dropped. 0.42 will display as 0.42 not 0.420 or 0.4200 or 0.4200000000.

The number has no idea that you want to display three digits, and cannot know. There is no way for each number to remember that at some point it came from you calling round().

If you need to format the number for display to a fixed size, don't use round, use one of the many different string methods:

>>> num = 0.42
>>> print("x = %.4f" % num)
x = 0.4200
>>> print("x = {:.8f}".format(num))
x = 0.42000000
>>> print(f"x = {num:.12f}")
x = 0.420000000000


This is for reporting bugs, not a help desk. In the future, it is best to check with more experienced programmers before reporting things as a bug. There are many useful forums such as the Python Discuss website, Reddit's r/learnpython, or the Python-List mailing list where people will be happy to help guide you whether you have discovered a bug or not.

https://www.python.org/community/forums/

This is *especially* important when it comes to numeric issues which can be tricky even for experienced coders.
msg402420 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-09-22 10:26
Oh, I forgot:

Python mailing lists and IRC:

https://www.python.org/community/lists/
https://www.python.org/community/irc/
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89426
2021-09-22 10:26:36steven.dapranosetmessages: + msg402420
2021-09-22 10:24:11steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg402418

resolution: not a bug
stage: resolved
2021-09-22 09:51:41kenfoscreate