Round a float to 2 decimals in Python

avatar

Borislav Hadzhiev

Last updated: Jul 12, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Round a float to 2 decimals in Python
  2. Round down float to 2 decimals in Python
  3. Round up float to 2 decimals in Python

Round a float to 2 decimals in Python #

Use the round() function to round a float to 2 decimals, e.g. result = round(4.5678, 2). The round() function will return the number rounded to 2 digits precision after the decimal point.

main.py
import math # ✅ round a float to 2 decimals result_1 = round(4.5678, 2) print(result_1) # 👉️ 4.57 result_2 = round(4.1234, 2) print(result_2) # 👉️ 4.12 # ------------------------------------- # ✅ print a float rounded to 2 decimals my_float = 4.5678 my_str_1 = f'{my_float:.2f}' print(my_str_1) # 👉️ '4.57' # ------------------------------------- # ✅ round DOWN float to 2 decimal places 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 # ------------------------------------- # ✅ round UP float to 2 decimal places 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 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
my_num = 1.45678 result_1 = round(my_num) print(result_1) # 👉️ 1 result_2 = round(my_num, 2) print(result_2) # 👉️ 1.46

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

main.py
my_float = 4.5678 my_str_1 = f'{my_float:.2f}' print(my_str_1) # 👉️ '4.57'
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 = 2.456789 result_1 = f'{my_float:.1f}' print(result_1) # 👉️ '2.5' result_2 = f'{my_float:.2f}' print(result_2) # 👉️ '2.46' result_3 = f'{my_float:.3f}' print(result_3) # 👉️ '2.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 float to 2 decimals in Python #

To round down a float to 2 decimals:

  1. Call the math.floor() method, passing it the number multiplied by 100.
  2. Divide the result by 100.
  3. The result of the calculation will be the float number rounded down to 2 decimals.
main.py
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 math.floor method returns the largest integer less than or equal to the provided number.

main.py
import math print(math.floor(15.999)) # 👉️ 15 print(math.floor(15.001)) # 👉️ 15
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 float to 2 decimal places.

main.py
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
We first multiply the number by 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:

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

Round up float to 2 decimals in Python #

To round up a float to 2 decimals:

  1. Call the math.ceil() method, passing it the number multiplied by 100.
  2. Divide the result by 100.
  3. The result of the calculation will be the float number rounded up to 2 decimals.
main.py
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 math.ceil method returns the smallest integer greater than or equal to the provided number.

main.py
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.

main.py
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
We first multiply the number by 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:

  1. Multiply the number by 100 and round the result up to the nearest integer.
  2. Divide the result by 100 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.