Last updated: Apr 9, 2024
Reading timeยท7 min
Use the round()
function to round a float to 1, 2 or 3 decimal places, e.g.
result = round(6.36789, 2)
.
The round()
function will round the floating-point number to the specified
number of decimal places and will return the result.
my_float = 6.36789 # โ round a float to 1, 2 or 3 decimal places (round()) result = round(my_float, 1) print(result) # ๐๏ธ 6.4 result = round(my_float, 2) print(result) # ๐๏ธ 6.37 result = round(my_float, 3) print(result) # ๐๏ธ 6.368
The example uses the round()
function to round a floating-point number to the
specified number of decimal places.
my_float = 6.36789 result = round(my_float, 3) print(result) # ๐๏ธ 6.368
The round() function takes the following 2 parameters:
Name | Description |
---|---|
number | the number to round to ndigits precision after the decimal |
ndigits | the number of digits after the decimal, the number should have after the operation (optional) |
The round
function returns the number rounded to ndigits
precision after the
decimal point.
If you need to call the round()
function on a list of floats, use a list
comprehension.
list_of_floats = [1.42342365, 3.438834, 5.5843854] result = [round(num, 2) for num in list_of_floats] print(result) # ๐๏ธ [1.42, 3.44, 5.58]
We used a list comprehension to iterate over the list of floating-point numbers
and passed each number to the round()
function.
Alternatively, you can use a formatted string literal.
my_float = 6.36789 result = f'{my_float:.1f}' print(result) # ๐๏ธ '6.4' result = f'{my_float:.2f}' print(result) # ๐๏ธ '6.37' result = f'{my_float:.3f}' print(result) # ๐๏ธ '6.368' number_of_decimals = 3 result = f'{my_float:.{number_of_decimals}f}' print(result) # ๐๏ธ '6.368'
f
.Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format-specific mini-language in expression blocks.
my_float = 6.36789 # ๐๏ธ Rounded to 1 decimals: 6.4 print(f'Rounded to 2 decimals: {my_float:.1f}') # ๐๏ธ Rounded to 2 decimals: 6.37 print(f'Rounded to 2 decimals: {my_float:.2f}') # ๐๏ธ Rounded to 3 decimals: 6.368 print(f'Rounded to 3 decimals: {my_float:.3f}')
The digit after the period is the number of decimal places the float should have.
If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.
my_float = 6.36789 number_of_decimals = 3 result = f'{my_float:.{number_of_decimals}f}' print(result) # ๐๏ธ '6.368'
If you need to round a list of floating-point numbers to N decimal places, use a list comprehension.
my_float = 6.36789 list_of_floats = [2.298438438, 4.5848548, 8.8347347] result = [f'{item:.3f}' for item in list_of_floats] print(result) # ๐๏ธ ['2.298', '4.585', '8.835']
We used a list comprehension to iterate over the list of floating-point numbers.
On each iteration, we use a formatted string literal to round the current float to 3 decimal places and return the result.
Rounding a float down to N decimals is a three-step process:
math.floor()
method, passing it the number multiplied by 100
.100
.import math def round_down_float_to_2_decimals(num): return math.floor(num * 100) / 100 print(round_down_float_to_2_decimals(4.6789)) # ๐๏ธ 4.67
The same approach can be used to round down a float to 3 decimal places.
import math def round_down_float_to_3_decimals(num): return math.floor(num * 1000) / 1000 print(round_down_float_to_3_decimals(4.6789)) # ๐๏ธ 4.678
The same approach can be used to round down a float to 1 decimal place.
import math def round_down_float_to_1_decimal(num): return math.floor(num * 10) / 10 print(round_down_float_to_1_decimal(2.6789)) # ๐๏ธ 2.6
The math.floor() method returns the largest integer less than or equal to the provided number.
import math print(math.floor(15.999)) # ๐๏ธ 15 print(math.floor(15.001)) # ๐๏ธ 15
math.floor
method rounds the number down.Here is a step-by-step example of rounding down a float to 2 decimal places.
import math print(4.4567 * 100) # ๐๏ธ 445.66999999999996 print(4.1234 * 100) # ๐๏ธ 412.34000000000003 print(math.floor(4.4567 * 100)) # ๐๏ธ 445 print(math.floor(4.1234 * 100)) # ๐๏ธ 412 print(math.floor(4.4567 * 100) / 100) # ๐๏ธ 4.45 print(math.floor(4.1234 * 100) / 100) # ๐๏ธ 4.12
100
and then divide by 100
to shift 2 decimal places to the left and right, so that math.floor()
works on the hundreds.This is a two-step process:
100
and round the result down to the nearest
integer.100
to round down the float to 2 decimal places.Rounding up a float to N decimals is a three-step process:
math.ceil()
method, passing it the number multiplied by 100
.100
.import math def round_up_float_to_2_decimals(num): return math.ceil(num * 100) / 100 print(round_up_float_to_2_decimals(4.6123)) # ๐๏ธ 4.62
The same approach can be used to round up a float to 3 decimal places.
import math def round_up_float_to_3_decimals(num): return math.ceil(num * 1000) / 1000 print(round_up_float_to_3_decimals(4.6123)) # ๐๏ธ 4.62
The same approach can be used to round up a float to 1 decimal place.
import math def round_up_float_to_1_decimal(num): return math.ceil(num * 10) / 10 print(round_up_float_to_1_decimal(2.6123)) # ๐๏ธ 2.7
The math.ceil() method returns the smallest integer greater than or equal to the provided number.
import math print(math.ceil(76.001)) # ๐๏ธ 77 print(math.ceil(76.999)) # ๐๏ธ 77
If the passed-in number has a fractional part, the math.ceil
method rounds the
number up.
Here is a step-by-step example of rounding up a float to 2 decimal places.
import math print(4.4567 * 100) # ๐๏ธ 445.66999999999996 print(4.1234 * 100) # ๐๏ธ 412.34000000000003 print(math.ceil(4.4567 * 100)) # ๐๏ธ 446 print(math.ceil(4.1234 * 100)) # ๐๏ธ 413 print(math.ceil(4.4567 * 100) / 100) # ๐๏ธ 4.46 print(math.ceil(4.1234 * 100) / 100) # ๐๏ธ 4.13
100
and then divide by 100
to shift 2 decimal places to the left and right, so that math.ceil()
works on the hundreds.This is a two-step process:
100
and round the result up to the nearest integer.100
to round up the float to 2 decimal places.To round a list of floats to 2 decimal places:
round()
function to round each float to 2 decimal places.list_of_floats = [2.4834843583, 4.28384291, 8.47989238] # โ Round a list of floats to 2 decimal places (round()) result = [round(item, 2) for item in list_of_floats] print(result) # ๐๏ธ [2.48, 4.28, 8.48] # ------------------------------------------ # โ Format a list of floats to 2 decimal places (f-string) result = [f'{item:.2f}' for item in list_of_floats] print(result) # ๐๏ธ ['2.48', '4.28', '8.48'] # ------------------------------------------ # โ Round a list of floats to 2 decimal places (numpy.around()) import numpy as np result = list(np.around(list_of_floats, 2)) print(result) # ๐๏ธ [2.48, 4.28, 8.48]
We used a list comprehension to iterate over the list of floating-point numbers.
list_of_floats = [2.4834843583, 4.28384291, 8.47989238] result = [round(item, 2) for item in list_of_floats] print(result) # ๐๏ธ [2.48, 4.28, 8.48]
On each iteration, we use the round()
function to round the current float to 2
decimal places.
The round() function takes the following 2 parameters:
Name | Description |
---|---|
number | the number to round to ndigits precision after the decimal |
ndigits | the number of digits after the decimal, the number should have after the operation (optional) |
The round
function returns the number rounded to ndigits
precision after the
decimal point.
Alternatively, you can use a formatted string literal.
This is a three-step process:
list_of_floats = [2.4834843583, 4.28384291, 8.47989238] result = [f'{item:.2f}' for item in list_of_floats] print(result) # ๐๏ธ ['2.48', '4.28', '8.48']
f
.Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format-specific mini-language in expression blocks.
my_float = 2.4834843583 print(f'{my_float:.2f}') # ๐๏ธ 2.48 print(f'{my_float:.3f}') # ๐๏ธ 2.483
The digit after the period is the number of decimal places the float should have.
If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.
list_of_floats = [2.4834843583, 4.28384291, 8.47989238] number_of_decimals = 2 result = [f'{item:.{number_of_decimals}f}' for item in list_of_floats] print(result) # ๐๏ธ ['2.48', '4.28', '8.48']
If you use NumPy, you can use the numpy.around()
method.
This is a two-step process:
numpy.around()
method to round each float in the list to 2 decimal
places.list()
class to convert the NumPy array to a list.import numpy as np list_of_floats = [2.4834843583, 4.28384291, 8.47989238] result = list(np.around(list_of_floats, 2)) print(result) # ๐๏ธ [2.48, 4.28, 8.48]
Make sure you have the NumPy module installed to be able to run the code sample:
pip install numpy pip3 install numpy
The numpy.around method takes an array-like object and rounds its items to the given number of decimals.
The decimals
argument is 0
by default.
The last step is to use the list()
class to convert the NumPy array to a list.
The list class takes an iterable and returns a list object.
I've also written an article on how to round a number to the nearest 5, 10, 100, 1000.
You can learn more about the related topics by checking out the following tutorials: