How to Round Floats to Decimal Places in Python
This guide explains how to round floating-point numbers to a specific number of decimal places in Python. We'll cover standard rounding, rounding up, rounding down, and formatting floats as strings with a fixed number of decimal places. We'll use built-in functions like round()
, f-strings, math.floor()
, math.ceil()
, and briefly cover NumPy for array operations.
Rounding with round()
The built-in round()
function is the standard way to round a float to a specific number of decimal places:
my_float = 6.36789
result = round(my_float, 1) # Round to 1 decimal place
print(result) # Output: 6.4
result = round(my_float, 2) # Round to 2 decimal places
print(result) # Output: 6.37
result = round(my_float, 3) # Round to 3 decimal places
print(result) # Output: 6.368
round(number, ndigits)
:
number
: The float (or integer) to round.ndigits
: The number of decimal places to round to. If omitted,round()
rounds to the nearest integer.
Formatting as String with F-strings (Recommended for Display)
For displaying a float with a specific number of decimal places (without necessarily changing the underlying numerical value), f-strings are the most concise and readable option:
my_float = 6.36789
result = f'{my_float:.1f}' # Format to 1 decimal place
print(result) # Output: '6.4' (string)
result = f'{my_float:.2f}' # Format to 2 decimal places
print(result) # Output: '6.37' (string)
result = f'{my_float:.3f}' # Format to 3 decimal places
print(result) # Output: '6.368' (string)
number_of_decimals = 3
result = f'{my_float:.{number_of_decimals}f}' # Dynamic precision
print(result) # Output: 6.368
f'{my_float:.2f}'
: This formatsmy_float
as a fixed-point number (f
) with 2 decimal places (.2
).- f-strings (and
str.format()
, which uses the same formatting syntax) produce strings, not floats. This is ideal for display purposes. - The width can also be a variable.
Rounding Down with math.floor()
To always round down to a specific number of decimal places, use math.floor()
:
import math
def round_down(num, decimals):
multiplier = 10 ** decimals
return math.floor(num * multiplier) / multiplier
print(round_down(4.6789, 2)) # Output: 4.67
print(round_down(4.6789, 3)) # Output: 4.678
print(round_down(2.6789, 1)) # Output: 2.6
- The number is first multiplied by 10 to the power of the required decimals, then rounded down to the nearest integer using
math.floor()
, and divided by the same power of 10 to retain the decimals required.
Rounding Up with math.ceil()
To always round up, use math.ceil()
:
import math
def round_up(num, decimals):
multiplier = 10 ** decimals
return math.ceil(num * multiplier) / multiplier
print(round_up(4.6123, 2)) # Output: 4.62
print(round_up(4.6123, 3)) # Output: 4.613
- The
math.ceil
method returns the lowest integer that is greater or equal to the number.
Rounding a List of Floats
Use a list comprehension for conciseness:
list_of_floats = [1.42342365, 3.438834, 5.5843854]
result = [round(num, 2) for num in list_of_floats] # Using round()
print(result) # Output: [1.42, 3.44, 5.58]
result_fstrings = [f'{item:.3f}' for item in list_of_floats] # Using f-string
print(result_fstrings) # Output: ['1.423', '3.439', '5.584']
- The list comprehension efficiently applies the operation to each element in the list.
Rounding with NumPy
If you're working with numerical data and already using NumPy, it provides efficient array-based rounding:
import numpy as np
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
result = list(np.around(list_of_floats, 2)) # Round using numpy.
print(result) # Output: [2.48, 4.28, 8.48]
- The code converts the numbers into a NumPy array, performs rounding using the
around
function, and then converts it back to a list.