In Python, we often come across floating-point values that contain many decimal places. While this is useful for high-precision calculations, it can sometimes create clutter and make it difficult to display or use these numbers effectively.
To make the reading and usage of floats more convenient, we can round them off to a specified number of decimal places. In this tutorial, we will discuss different methods for rounding floats in Python.
Method 1: Using the round() Function
Python has a built-in function called round()
which can be used to round off floating-point values. The syntax of this function is as follows:
1 |
round(number, ndigits) |
Here, number
is the floating-point value we want to round, and ndigits
is the number of decimal places to which we want to round the number. If ndigits
is not provided, it defaults to 0, and the function returns the nearest integer value.
Let’s see an example:
1 2 3 |
float_number = 3.14159 rounded_number = round(float_number, 2) print(rounded_number) |
Output:
3.14
Method 2: Using String Formatting
We can also round floats using string formatting. Here, we use the format specifier f
to specify the number of decimal places we want to round the float to. The syntax for this is:
1 |
"{:.nf}".format(number) |
Here, n
is the number of decimal places we want to round the float to.
Let’s see an example:
1 2 3 |
float_number = 3.14159 rounded_number = "{:.2f}".format(float_number) print(rounded_number) |
Output:
3.14
Note that this method returns a string representation of the rounded number. If you need the rounded number as a float, you can use the float()
function to convert the string back to a float.
Method 3: Using Mathematical Functions
We can also round floats using mathematical functions from the Python math module. The math module provides two functions, floor()
and ceil()
, which can be used to round down and round up a float respectively. To round a float to a specified number of decimal places, we can make use of these functions along with the following formula:
1 |
floor(number * 10 <strong> ndigits) / 10 </strong> ndigits |
1 |
ceil(number * 10 <strong> ndigits) / 10 </strong> ndigits |
Let’s see an example:
1 2 3 4 5 6 7 8 |
import math float_number = 3.14159 ndigits = 2 rounded_number_floor = math.floor(float_number * 10 **<strong> ndigits) / 10 </strong>** ndigits rounded_number_ceil = math.ceil(float_number * 10 ** <strong>ndigits) / 10 </strong>** ndigits print(rounded_number_floor) print(rounded_number_ceil) |
Output:
3.14 3.15
Full Code
Here’s the full code for all three methods of rounding floats in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import math float_number = 3.14159 # Method 1: Using round() function rounded_number1 = round(float_number, 2) print(rounded_number1) # Method 2: Using string formatting rounded_number2 = "{:.2f}".format(float_number) print(rounded_number2) # Method 3: Using mathematical functions ndigits = 2 rounded_number_floor = math.floor(float_number * 10 ** <strong>ndigits) / 10 </strong>** ndigits rounded_number_ceil = math.ceil(float_number * 10 ** <strong>ndigits) / 10</strong> ** ndigits print(rounded_number_floor) print(rounded_number_ceil) |
Output:
3.14 3.14 3.14 3.15
Conclusion
In this tutorial, we discussed three different methods to round floats in Python: using the round()
function, using string formatting, and using mathematical functions from the math module. Depending on your requirements and the situation, you can opt for any of these methods to round your floating-point values in Python.