Use Python built-in round() function to round decimal. This method rounds off a number to the given number of digits and makes rounding numbers easier.
round(number, number of digits)
If the number of digits is not provided for round-off, the function rounds off the number n to the nearest integer.
Python round decimal example
Simple example code.
from decimal import Decimal
d1 = Decimal(21.73696)
d2 = Decimal(21.4)
print(round(d1, 3))
print(round(d1))
Output:
How to round a Python Decimal to 2 decimal places?
Answer: Since Python 3.3 you can use round()
with a Decimal
and it will return you a Decimal
.
from decimal import Decimal
res = round(Decimal('3.14159265359'), 3)
print(res)
Output: 3.142
Do comment if you have any doubts or suggestions on this Python round topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.