Python round() – Round to 2 Decimal Places
Python round() function rounds a floating point number to specific precision. In this tutorial, we will learn how to round a floating point number to a precision of two decimal digits.
Example 1 – Round Number to 2 Decimal Places
In this example, we will initialize a variable pi
with a floating value and then round of its value to two decimal points.
Python Program
pi = 3.141592653589793238
pi_r = round(pi, 2)
print(pi_r)
Output
3.14
In the following example, we compute rounded value of different floating point values.
Example 2 – Round Number with only 1 Decimal to 2 Decimal Places
In this example, we will initialize a variable pi
with a floating value of only one decimal point. And then try to round of its value to two decimal points.
Python Program
pi = 3.1
pi_r = round(pi, 2)
print(pi_r)
Output
3.1
If the number we provide has fewer decimal digits that the specified precision, then round() function returns the number as is.
Conclusion
In this Python Tutorial, we learned how to round of a number to 2 decimal digits using round function.