Last updated: Apr 9, 2024
Reading timeยท16 min
Use the round()
function to round a float to the nearest 10th (0.1), e.g.
result = round(4.5678, 1)
.
The round()
function will return the number rounded to 1-digit precision
after the decimal point.
# โ round a float to the nearest 10th (0.1) result_1 = round(4.5678, 1) print(result_1) # ๐๏ธ 4.6 result_2 = round(4.1234, 1) print(result_2) # ๐๏ธ 4.1 # ------------------------------- # โ print a float rounded to the nearest 10th (0.1) my_float = 4.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # ๐๏ธ '4.6' my_str_2 = f'{my_float:.2f}' print(my_str_2) # ๐๏ธ '4.57'
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) |
round
function returns the number rounded to ndigits
precision after the decimal point.If ndigits
is omitted, the function returns the nearest integer.
my_num = 3.456 result_1 = round(my_num) print(result_1) # ๐๏ธ 3 result_2 = round(my_num, 1) print(result_2) # ๐๏ธ 3.5
If you need to print a floating-point number rounded to the nearest 10th (0.1), use a formatted string literal.
my_float = 4.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # ๐๏ธ '4.6' my_str_2 = f'{my_float:.2f}' print(my_str_2) # ๐๏ธ '4.57'
f
.my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
We are also able to use the format specification mini-language in expressions in f-strings.
my_float = 1.45678 result_1 = f'{my_float:.1f}' print(result_1) # ๐๏ธ '1.5' result_2 = f'{my_float:.2f}' print(result_2) # ๐๏ธ '1.46' result_3 = f'{my_float:.3f}' print(result_3) # ๐๏ธ '1.457'
The f
character between the curly braces stands for fixed-point notation.
The character formats the number as a decimal number with the specified number of digits following the decimal point.
To round a float to the nearest 0.5:
round()
function passing it the number multiplied by 2
.2
.0.5
.import math # โ Round number to nearest 0.5 def round_to_nearest_half_int(num): return round(num * 2) / 2 print(round_to_nearest_half_int(3.1)) # ๐๏ธ 3.0 print(round_to_nearest_half_int(3.7)) # ๐๏ธ 3.5 # -------------------------------------- # โ Round number UP to nearest 0.5 def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # ๐๏ธ 3.5 print(round_up_to_nearest_half_int(3.7)) # ๐๏ธ 4.0 # -------------------------------------- # โ Round number DOWN to nearest 0.5 def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # ๐๏ธ 3.5 print(round_down_to_nearest_half_int(3.4)) # ๐๏ธ 3.0
We used the round()
function to round a number to the nearest 0.5.
When passed a single argument, the round() function rounds to the nearest integer.
print(round(7.4)) # ๐๏ธ 7 print(round(7.6)) # ๐๏ธ 8
Here is a step-by-step example of rounding a number up to the nearest 0.5
.
print(3.1 * 2) # ๐๏ธ 6.2 print(3.7 * 2) # ๐๏ธ 7.4 print(round(3.1 * 2)) # ๐๏ธ 6 print(round(3.7 * 2)) # ๐๏ธ 7 print(round(3.1 * 2) / 2) # ๐๏ธ 3.0 print(round(3.7 * 2) / 2) # ๐๏ธ 3.5
This is a two-step process:
2
and round the result to the nearest integer.2
to get the number rounded to the nearest 0.5
.Use the math.ceil()
method if you need to round a float up to the nearest 0.5.
import math def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # ๐๏ธ 3.5 print(round_up_to_nearest_half_int(3.7)) # ๐๏ธ 4.0 print(round_up_to_nearest_half_int(16.2)) # ๐๏ธ 16.5
The math.ceil method returns the smallest integer greater than or equal to the provided number.
import math print(math.ceil(3.1)) # ๐๏ธ 4 print(math.ceil(3.9)) # ๐๏ธ 4
math.ceil
method rounds the number up.Here is a step-by-step example of rounding a number up to the nearest 0.5
.
import math print(6.1 * 2) # ๐๏ธ 12.2 print(6.6 * 2) # ๐๏ธ 13.2 print(math.ceil(6.1 * 2)) # ๐๏ธ 13 print(math.ceil(6.6 * 2)) # ๐๏ธ 14 print(math.ceil(6.1 * 2) / 2) # ๐๏ธ 6.5 print(math.ceil(6.6 * 2) / 2) # ๐๏ธ 7.0
This is a two-step process:
2
and round the result up to the nearest integer.2
to get the number rounded up to the nearest 0.5
.Use the math.floor()
method to round a number down to the nearest 0.5.
import math def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # ๐๏ธ 3.5 print(round_down_to_nearest_half_int(3.4)) # ๐๏ธ 3.0
The math.floor method returns the largest integer less than or equal to the provided number.
import math print(math.floor(3.9)) # ๐๏ธ 3 print(math.floor(3.1)) # ๐๏ธ 3
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 0.5
.
import math print(5.9 * 2) # ๐๏ธ 11.8 print(5.1 * 2) # ๐๏ธ 10.2 print(math.floor(5.9 * 2)) # ๐๏ธ 11 print(math.floor(5.1 * 2)) # ๐๏ธ 10 print(math.floor(5.9 * 2) / 2) # ๐๏ธ 5.5 print(math.floor(5.1 * 2) / 2) # ๐๏ธ 5.0
This is a two-step process:
2
and round the result down to the nearest integer.2
to get the number rounded down to the nearest 0.5
.To round a number to the nearest 5:
round()
function passing it the number divided by 5
.5
.5
.import math # โ Round number to nearest 5 def round_to_nearest_5(num): return round(num / 5) * 5 print(round_to_nearest_5(2)) # ๐๏ธ 0 print(round_to_nearest_5(8)) # ๐๏ธ 10 print(round_to_nearest_5(26)) # ๐๏ธ 25 # -------------------------------------- # โ Round number UP to nearest 5 def round_up_to_nearest_5(num): return math.ceil(num / 5) * 5 print(round_up_to_nearest_5(23)) # ๐๏ธ 25 print(round_up_to_nearest_5(57)) # ๐๏ธ 60 # -------------------------------------- # โ Round number DOWN to nearest 5 def round_down_to_nearest_5(num): return math.floor(num / 5) * 5 print(round_down_to_nearest_5(121)) # ๐๏ธ 120 print(round_down_to_nearest_5(129)) # ๐๏ธ 125
We used the round()
function to round a number to the nearest 5.
When passed a single argument, the round function rounds to the nearest integer.
print(round(14.4)) # ๐๏ธ 14 print(round(14.6)) # ๐๏ธ 15
Here is a step-by-step example of rounding a number up to the nearest five.
print(24 / 5) # ๐๏ธ 4.8 print(38 / 5) # ๐๏ธ 7.6 print(round(24 / 5)) # ๐๏ธ 5 print(round(38 / 5)) # ๐๏ธ 8 print(round(24 / 5) * 5) # ๐๏ธ 25 print(round(38 / 5) * 5) # ๐๏ธ 40
This is a two-step process:
5
and round the result to the nearest integer.5
to get the number rounded to the nearest 5
.Use the math.ceil()
method to round a number up to the nearest 5.
import math def round_up_to_nearest_5(num): return math.ceil(num / 5) * 5 print(round_up_to_nearest_5(23)) # ๐๏ธ 25 print(round_up_to_nearest_5(57)) # ๐๏ธ 60
The math.ceil() method returns the smallest integer greater than or equal to the provided number.
import math print(math.ceil(6.01)) # ๐๏ธ 7 print(math.ceil(6.99)) # ๐๏ธ 7
math.ceil
method rounds the number up.Here is a step-by-step example of rounding a number up to the nearest five.
import math print(142 / 5) # ๐๏ธ 28.4 print(148 / 5) # ๐๏ธ 29.6 print(math.ceil(142 / 5)) # ๐๏ธ 29 print(math.ceil(148 / 5)) # ๐๏ธ 30 print(math.ceil(142 / 5) * 5) # ๐๏ธ 145 print(math.ceil(148 / 5) * 5) # ๐๏ธ 150
This is a two-step process:
5
and round the result up to the nearest integer.5
to get the number rounded up to the nearest 5
.Use math.floor()
if you need to round down to the nearest 5.
import math def round_down_to_nearest_5(num): return math.floor(num / 5) * 5 print(round_down_to_nearest_5(121)) # ๐๏ธ 120 print(round_down_to_nearest_5(129)) # ๐๏ธ 125
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(3.99)) # ๐๏ธ 3 print(math.floor(3.01)) # ๐๏ธ 3
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 5.
import math print(49 / 5) # ๐๏ธ 9.8 print(56 / 5) # ๐๏ธ 11.2 print(math.floor(49 / 5)) # ๐๏ธ 9 print(math.floor(56 / 5)) # ๐๏ธ 11 print(math.floor(49 / 5) * 5) # ๐๏ธ 45 print(math.floor(56 / 5) * 5) # ๐๏ธ 55
This is a two-step process:
5
and round the result down to the nearest integer.5
to get the number rounded down to the nearest 5
.Use the round()
function to round a number to the nearest 10.
import math # โ Round number to nearest 10 num_1 = 6 result_1 = round(num_1, -1) print(result_1) # ๐๏ธ 10 num_2 = 4 result_2 = round(num_2, -1) print(result_2) # ๐๏ธ 0 # -------------------------------------- # โ Round number UP to nearest 10 def round_up_to_nearest_10(num): return math.ceil(num / 10) * 10 print(round_up_to_nearest_10(3)) # ๐๏ธ 10 print(round_up_to_nearest_10(1)) # ๐๏ธ 10 # -------------------------------------- # โ Round number DOWN to nearest 10 def round_down_to_nearest_10(num): return math.floor(num / 10) * 10 print(round_down_to_nearest_10(19)) # ๐๏ธ 10 print(round_down_to_nearest_10(27)) # ๐๏ธ 20
We used the round()
function to round a number to the nearest 10.
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) |
ndigits
is a negative number, the round()
function rounds to the left of the decimal.If ndigits
is -1
, it rounds to the closest multiple of 10
. When ndigits
is -2
, the function rounds to the nearest 100
, etc.
print(round(157, -1)) # ๐๏ธ 160 print(round(157, -2)) # ๐๏ธ 200
Use the math.ceil()
method to round up to the nearest 10.
import math def round_up_to_nearest_10(num): return math.ceil(num / 10) * 10 print(round_up_to_nearest_10(3)) # ๐๏ธ 10 print(round_up_to_nearest_10(1)) # ๐๏ธ 10 print(round_up_to_nearest_10(21)) # ๐๏ธ 30
The math.ceil
method returns the smallest integer greater than or equal to the
provided number.
import math print(math.ceil(5.001)) # ๐๏ธ 6 print(math.ceil(5.99)) # ๐๏ธ 6
If the passed-in number has a fractional part, the math.ceil
method rounds the
number up.
import math my_num = math.ceil(3 / 10) # ๐๏ธ 1 print(my_num) # ๐๏ธ 1 result = my_num * 10 print(result) # ๐๏ธ 10
Here is a step-by-step example of rounding a number up to the nearest 10.
import math print(21 / 10) # ๐๏ธ 2.1 print(40 / 10) # ๐๏ธ 4.0 print(math.ceil(21 / 10)) # ๐๏ธ 3 print(math.ceil(40 / 10)) # ๐๏ธ 4 print(math.ceil(21 / 10) * 10) # ๐๏ธ 30 print(math.ceil(40 / 10) * 10) # ๐๏ธ 40
10
and then multiply with 10
to shift 1 decimal place to the right and left, so that math.ceil()
works on the tens.This is a two-step process:
10
and round the result up to the nearest integer.10
to get the number rounded up to the nearest 10
.Use the math.floor()
method to round down to the nearest 10.
import math def round_down_to_nearest_10(num): return math.floor(num / 10) * 10 print(round_down_to_nearest_10(19)) # ๐๏ธ 10 print(round_down_to_nearest_10(27)) # ๐๏ธ 20 print(round_down_to_nearest_10(42)) # ๐๏ธ 40
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(3.999)) # ๐๏ธ 3 print(math.floor(3.001)) # ๐๏ธ 3
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 10.
import math print(34 / 10) # ๐๏ธ 3.4 print(50 / 10) # ๐๏ธ 5.0 print(math.floor(34 / 10)) # ๐๏ธ 3 print(math.floor(50 / 10)) # ๐๏ธ 5 print(math.floor(34 / 10) * 10) # ๐๏ธ 30 print(math.floor(50 / 10) * 10) # ๐๏ธ 50
10
and then multiply with 10
to shift 1 decimal place to the right and left, so that math.floor()
works on the tens.This is a two-step process:
10
and round the result down to the nearest integer.10
to get the number rounded down to the nearest
10
.Use the round()
function if you need to round a number to the nearest 100.
import math # โ Round number to nearest 100 num_1 = 237 result_1 = round(num_1, -2) print(result_1) # ๐๏ธ 200 num_2 = 278 result_2 = round(num_2, -2) print(result_2) # ๐๏ธ 300 # -------------------------------------- # โ Round number UP to nearest 100 def round_up_to_nearest_100(num): return math.ceil(num / 100) * 100 print(round_up_to_nearest_100(311)) # ๐๏ธ 400 print(round_up_to_nearest_100(1)) # ๐๏ธ 100 # -------------------------------------- # โ Round number DOWN to nearest 100 def round_down_to_nearest_100(num): return math.floor(num / 100) * 100 print(round_down_to_nearest_100(546)) # ๐๏ธ 500 print(round_down_to_nearest_100(599)) # ๐๏ธ 500
We used the round()
function to round a number to the nearest 100.
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) |
ndigits
is a negative number, the round()
function rounds to the left of the decimal.If ndigits
is -1
, it rounds to the closest multiple of 10
. When ndigits
is -2
, the function rounds to the nearest 100
, etc.
print(round(346, -1)) # ๐๏ธ 350 print(round(346, -2)) # ๐๏ธ 300
Use the math.ceil()
method if you need to round a number up to the
nearest 100.
import math def round_up_to_nearest_100(num): return math.ceil(num / 100) * 100 print(round_up_to_nearest_100(311)) # ๐๏ธ 400 print(round_up_to_nearest_100(1)) # ๐๏ธ 100
The math.ceil
method returns the smallest integer greater than or equal to the
provided number.
import math print(math.ceil(123.001)) # ๐๏ธ 124 print(math.ceil(123.999)) # ๐๏ธ 124
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 a number up to the nearest hundred.
import math print(346 / 100) # ๐๏ธ 3.46 print(600 / 100) # ๐๏ธ 6.0 print(math.ceil(346 / 100)) # ๐๏ธ 4 print(math.ceil(600 / 100)) # ๐๏ธ 6 print(math.ceil(346 / 100) * 100) # ๐๏ธ 400 print(math.ceil(600 / 100) * 100) # ๐๏ธ 600
100
and then multiply with 100
to shift 2 decimal places to the right and left, 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 get the number rounded up to the nearest
100
.Use the math.floor()
method to round a number down to the nearest 100.
import math def round_down_to_nearest_100(num): return math.floor(num / 100) * 100 print(round_down_to_nearest_100(546)) # ๐๏ธ 500 print(round_down_to_nearest_100(599)) # ๐๏ธ 500 print(round_down_to_nearest_100(775)) # ๐๏ธ 700
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(15.001)) # ๐๏ธ 15 print(math.floor(15.999)) # ๐๏ธ 15
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 100.
print(488 / 100) # ๐๏ธ 4.88 print(251 / 100) # ๐๏ธ 2.51 print(math.floor(488 / 100)) # ๐๏ธ 4 print(math.floor(251 / 100)) # ๐๏ธ 2 print(math.floor(488 / 100) * 100) # ๐๏ธ 400 print(math.floor(251 / 100) * 100) # ๐๏ธ 200
100
and then multiply with 100
to shift 2 decimal places to the right and left, 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 get the number rounded down to the nearest
100
.Use the round()
function to round a number to the nearest 500.
import math # โ Round number to nearest 500 def round_to_nearest_500(num): return round(num / 500) * 500 print(round_to_nearest_500(777)) # ๐๏ธ 1000 print(round_to_nearest_500(1)) # ๐๏ธ 0 print(round_to_nearest_500(1400)) # ๐๏ธ 1500 # -------------------------------------- # โ Round number UP to nearest 500 def round_up_to_nearest_500(num): return math.ceil(num / 500) * 500 print(round_up_to_nearest_500(640)) # ๐๏ธ 1000 print(round_up_to_nearest_500(1)) # ๐๏ธ 500 # -------------------------------------- # โ Round number DOWN to nearest 500 def round_down_to_nearest_500(num): return math.floor(num / 500) * 500 print(round_down_to_nearest_500(999)) # ๐๏ธ 500 print(round_down_to_nearest_500(1840)) # ๐๏ธ 1500
We used the round()
function to round a number to the nearest 500.
When passed a single argument, the round function rounds to the nearest integer.
print(round(13.4)) # ๐๏ธ 13 print(round(13.6)) # ๐๏ธ 14
Here is a step-by-step example of rounding a number up to the nearest five hundred.
print(1750 / 500) # ๐๏ธ 3.5 print(1400 / 500) # ๐๏ธ 2.8 print(round(1750 / 500)) # ๐๏ธ 4 print(round(1400 / 500)) # ๐๏ธ 3 print(round(1750 / 500) * 500) # ๐๏ธ 2000 print(round(1400 / 500) * 500) # ๐๏ธ 1500
This is a two-step process:
500
and round the result to the nearest integer.500
to get the number rounded to the nearest 500
.Use the math.ceil()
method to round a number up to the nearest 500.
import math def round_up_to_nearest_500(num): return math.ceil(num / 500) * 500 print(round_up_to_nearest_500(640)) # ๐๏ธ 1000 print(round_up_to_nearest_500(1)) # ๐๏ธ 500
The math.ceil
method returns the smallest integer greater than or equal to the
provided number.
import math print(math.ceil(456.001)) # ๐๏ธ 457 print(math.ceil(456.999)) # ๐๏ธ 457
math.ceil
method rounds the number up.Here is a step-by-step example of rounding a number up to the nearest five hundred.
import math print(1346 / 500) # ๐๏ธ 2.692 print(1600 / 500) # ๐๏ธ 3.2 print(math.ceil(1346 / 500)) # ๐๏ธ 3 print(math.ceil(1600 / 500)) # ๐๏ธ 4 print(math.ceil(1346 / 500) * 500) # ๐๏ธ 1500 print(math.ceil(1600 / 500) * 500) # ๐๏ธ 2000
This is a two-step process:
500
and round the result up to the nearest integer.500
to get the number rounded up to the nearest
500
.Use the math.floor()
method to round a number down to the nearest 500.
import math def round_down_to_nearest_500(num): return math.floor(num / 500) * 500 print(round_down_to_nearest_500(999)) # ๐๏ธ 500 print(round_down_to_nearest_500(1840)) # ๐๏ธ 1500 print(round_down_to_nearest_500(2840)) # ๐๏ธ 2500
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(25.999)) # ๐๏ธ 25 print(math.floor(25.001)) # ๐๏ธ 25
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 500.
import math print(4880 / 500) # ๐๏ธ 9.76 print(2510 / 500) # ๐๏ธ 5.02 print(math.floor(4880 / 500)) # ๐๏ธ 9 print(math.floor(2510 / 500)) # ๐๏ธ 5 print(math.floor(4880 / 500) * 500) # ๐๏ธ 4500 print(math.floor(2510 / 500) * 500) # ๐๏ธ 2500
This is a two-step process:
500
and round the result down to the nearest integer.500
to get the number rounded down to the nearest
500
.Use the round()
function to round a number to the nearest 1000.
import math # โ Round number to nearest 1000 num_1 = 4678 result_1 = round(num_1, -3) print(result_1) # ๐๏ธ 5000 num_2 = 4432 result_2 = round(num_2, -3) print(result_2) # ๐๏ธ 4000 # -------------------------------------- # โ Round number UP to nearest 1000 def round_up_to_nearest_1000(num): return math.ceil(num / 1000) * 1000 print(round_up_to_nearest_1000(3100)) # ๐๏ธ 4000 print(round_up_to_nearest_1000(1)) # ๐๏ธ 1000 # -------------------------------------- # โ Round number DOWN to nearest 1000 def round_down_to_nearest_1000(num): return math.floor(num / 1000) * 1000 print(round_down_to_nearest_1000(5999)) # ๐๏ธ 5000 print(round_down_to_nearest_1000(5004)) # ๐๏ธ 5000
We used the round()
function to round a number to the nearest 1000.
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) |
ndigits
is a negative number, the round()
function rounds to the left of the decimal.ndigits
is -1
, the function rounds to the closest multiple of 10
.ndigits
is -2
, it rounds to the nearest 100
.ndigits
is -3
, it rounds to the nearest 1000
, etc.print(round(3456, -1)) # ๐๏ธ 3460 print(round(3456, -2)) # ๐๏ธ 3500 print(round(3456, -3)) # ๐๏ธ 3000
Use the math.ceil()
method to round a number up to the nearest 1000.
import math def round_up_to_nearest_1000(num): return math.ceil(num / 1000) * 1000 print(round_up_to_nearest_1000(3100)) # ๐๏ธ 4000 print(round_up_to_nearest_1000(1)) # ๐๏ธ 1000 print(round_up_to_nearest_1000(2350)) # ๐๏ธ 3000
The math.ceil
method returns the smallest integer greater than or equal to the
provided number.
import math print(math.ceil(1234.001)) # ๐๏ธ 1235 print(math.ceil(1234.999)) # ๐๏ธ 1235
math.ceil
method rounds the number up.Here is a step-by-step example of rounding a number up to the nearest thousand.
import math print(4258 / 1000) # ๐๏ธ 4.258 print(5600 / 1000) # ๐๏ธ 5.6 print(math.ceil(4258 / 1000)) # ๐๏ธ 5 print(math.ceil(5600 / 1000)) # ๐๏ธ 6 print(math.ceil(4258 / 1000) * 1000) # ๐๏ธ 5000 print(math.ceil(5600 / 1000) * 1000) # ๐๏ธ 6000
1000
and then multiply with 1000
to shift 3 decimal places to the right and left, so that math.ceil()
works on the thousands.This is a two-step process:
1000
and round the result up to the nearest integer.1000
to get the number rounded up to the nearest
1000
.Use the math.floor()
method to round a number down to the nearest 1000.
import math def round_down_to_nearest_1000(num): return math.floor(num / 1000) * 1000 print(round_down_to_nearest_1000(5999)) # ๐๏ธ 5000 print(round_down_to_nearest_1000(5004)) # ๐๏ธ 5000 print(round_down_to_nearest_1000(7900)) # ๐๏ธ 7000
The math.floor
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(13.999)) # ๐๏ธ 13 print(math.floor(13.001)) # ๐๏ธ 13
math.floor
method rounds the number down.Here is a step-by-step example of rounding a number down to the nearest 1000.
import math print(5900 / 1000) # ๐๏ธ 5.9 print(4300 / 1000) # ๐๏ธ 4.3 print(math.floor(5900 / 1000)) # ๐๏ธ 5 print(math.floor(4300 / 1000)) # ๐๏ธ 4 print(math.floor(5900 / 1000) * 1000) # ๐๏ธ 5000 print(math.floor(4300 / 1000) * 1000) # ๐๏ธ 4000
1000
and then multiply with 1000
to shift 3 decimal places to the right and left, so that math.floor()
works on the thousands.This is a two-step process:
1000
and round the result down to the nearest integer.1000
to get the number rounded down to the nearest
1000
.Use the round()
function to round a number to the nearest even number.
import math # โ round number to nearest even number def round_to_nearest_even_number(num): return round(num / 2) * 2 print(round_to_nearest_even_number(3.1)) # ๐๏ธ 4 print(round_to_nearest_even_number(8.6)) # ๐๏ธ 8 # ----------------------------------------------- # โ round a number UP to the nearest even number def round_up_to_nearest_even_number(num): return math.ceil(num / 2) * 2 print(round_up_to_nearest_even_number(3.1)) # ๐๏ธ 4 print(round_up_to_nearest_even_number(8.6)) # ๐๏ธ 10 # ----------------------------------------------- # โ round a number DOWN to the nearest even number def round_down_to_nearest_even_number(num): return math.floor(num / 2) * 2 print(round_down_to_nearest_even_number(3.1)) # ๐๏ธ 2 print(round_down_to_nearest_even_number(8.6)) # ๐๏ธ 8
We used the round()
function to round a number to the nearest even integer.
When passed a single argument, the round() function rounds to the nearest integer.
print(round(22.4)) # ๐๏ธ 22 print(round(22.6)) # ๐๏ธ 23
This is a two-step process:
2
and round the result to the nearest integer.2
to get the nearest even integer.Use the math.ceil()
method to round a number up to the nearest even number.
import math def round_up_to_nearest_even_number(num): return math.ceil(num / 2) * 2 print(round_up_to_nearest_even_number(3.1)) # ๐๏ธ 4 print(round_up_to_nearest_even_number(8.6)) # ๐๏ธ 10
The math.ceil
method returns the smallest integer greater than or equal to the
provided number.
import math print(math.ceil(14.01)) # ๐๏ธ 15 print(math.ceil(14.99)) # ๐๏ธ 15
math.ceil
method rounds the number up.This is a two-step process:
2
and round the result up to the nearest integer.2
to get the next even number.Use the math.floor()
method to round a number down to the nearest even number.
import math def round_down_to_nearest_even_number(num): return math.floor(num / 2) * 2 print(round_down_to_nearest_even_number(3.1)) # ๐๏ธ 2 print(round_down_to_nearest_even_number(8.6)) # ๐๏ธ 8
The math.floor()
method returns the largest integer less than or equal to the
provided number.
import math print(math.floor(9.99)) # ๐๏ธ 9 print(math.floor(9.01)) # ๐๏ธ 9
math.floor
method rounds the number down.This is a two-step process:
2
and round the result down to the nearest integer.2
to get the number rounded down to the nearest even
integer.I've also written an article on how to round a float to N decimal places.
You can learn more about the related topics by checking out the following tutorials: