Round a number to 1 decimal place in Python

avatar

Borislav Hadzhiev

Last updated: Jul 13, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Round a number to 1 decimal place in Python
  2. Round down a number to 1 decimal place in Python
  3. Round up a number to 1 decimal place in Python

Round a number to 1 decimal place in Python #

Use the round() function to round a number to 1 decimal, e.g. result = round(2.5678, 1). The round() function will return the number rounded to 1 digit precision after the decimal point.

main.py
import math # ✅ round number to 1 decimal place result_1 = round(2.5678, 1) print(result_1) # 👉️ 2.6 result_2 = round(2.1234, 1) print(result_2) # 👉️ 2.1 # ------------------------------------- # ✅ print a float rounded to 1 decimal my_float = 2.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # 👉️ '2.6' # ------------------------------------- # ✅ round DOWN float to 1 decimal place 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 # ------------------------------------- # ✅ round UP float to 1 decimal place 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 round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe 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 ndigits is omitted, the function returns the nearest integer.

main.py
print(round(1.56789, 1)) # 👉️ 1.6 print(round(1.56789)) # 👉️ 2

If you need to print a floating-point number rounded to 2 decimal places, use a formatted string literal.

main.py
my_float = 2.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # 👉️ '2.6'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
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.

main.py
my_float = 6.456789 result_1 = f'{my_float:.1f}' print(result_1) # 👉️ '6.5' result_2 = f'{my_float:.2f}' print(result_2) # 👉️ '6.46' result_3 = f'{my_float:.3f}' print(result_3) # 👉️ '6.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.

Round down a number to 1 decimal place in Python #

To round down a number to 1 decimal:

  1. Call the math.floor() method, passing it the number multiplied by 10.
  2. Divide the result by 10.
  3. The result of the calculation will be the float number rounded down to 1 decimal.
main.py
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.

main.py
import math print(math.floor(6.999)) # 👉️ 6 print(math.floor(6.001)) # 👉️ 6
If the passed in number has a fractional part, the math.floor method rounds the number down.

Here is a step-by-step example of rounding down a number to 1 decimal place.

main.py
import math print(4.567 * 10) # 👉️ 45.67 print(4.1234 * 10) # 👉️ 41.234 print(math.floor(4.567 * 10)) # 👉️ 45 print(math.floor(4.1234 * 10)) # 👉️ 41 print(math.floor(4.567 * 10) / 10) # 👉️ 4.5 print(math.floor(4.1234 * 10) / 10) # 👉️ 4.1
We first multiply the number by 10 and then divide by 10 to shift 1 decimal place to the left and right, so that math.floor() works on the tens.

This is a two step process:

  1. Multiply the number by 10 and round the result down to the nearest integer.
  2. Divide the result by 10 to round down the float to 1 decimal place.

Round up a number to 1 decimal place in Python #

To round up a number to 1 decimal:

  1. Call the math.ceil() method, passing it the number multiplied by 10.
  2. Divide the result by 10.
  3. The result of the calculation will be the float number rounded down to 1 decimal.
main.py
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.

main.py
import math print(math.ceil(18.001)) # 👉️ 19 print(math.ceil(18.999)) # 👉️ 19

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 1 decimal place.

main.py
import math print(2.6123 * 10) # 👉️ 26.122999999999998 print(4.1234 * 10) # 👉️ 41.234 print(math.ceil(2.6123 * 10)) # 👉️ 27 print(math.ceil(4.1234 * 10)) # 👉️ 42 print(math.ceil(2.6123 * 10) / 10) # 👉️ 2.7 print(math.ceil(4.1234 * 10) / 10) # 👉️ 4.2
We first multiply the number by 10 and then divide by 10 to shift 2 decimal places to the left and right, so that math.ceil() works on the tens.

This is a two step process:

  1. Multiply the number by 10 and round the result up to the nearest integer.
  2. Divide the result by 10 to round up the float to 2 decimal places.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.