How to round to two decimal places in Python

In this tutorial, learn how to round float to 2 decimal places in Python. The short answer is: use Python round() to change to 2 decimal places. The two decimal places are the two digits after the decimal point in a float variable.

You can also round float to 3 decimal places after the decimal point. The round() is the commonly known function of Python to perform this task. However, you will learn some other methods with other functions of Python.

So, let’s start converting the float to 2 decimal places with the examples given here.

Round Float to 2 Decimal Places in Python

To round the float value to 2 decimal places, you have to use the Python round(). The round function is the common function to use and requires only two arguments. If you want to round to 2 decimal places, you have to pass 2 as the value of the second argument. The first argument is the string of Python that you want to convert.

myFloat = 23.98765; print(round(myFloat, 2));

print(round(myFloat, 2));

Output

23.99

The above example showing the rounded string to 2 decimal places. The second decimal place number is 8 in the example. However, after the round conversion, you will get 9 as the second decimal number. This is just because of the round() increase the value if it is 5 or more than 5.

Note: If the number in the third decimal place is more than 5, the 2nd decimal place value increases to 1 in the output.

How to Round Float to 3 Decimal Places in Python

If you want to round the float value to 3 decimal places. You have to use the same round() and pass 3 as the second argument. The first argument is the same float variable you have to pass to the function.

myFloat = 23.98765; print(round(myFloat, 3));

print(round(myFloat, 3));

Output

23.988

The above example showing the rounded 3 decimal place output of the float. The third digit after decimal also gets the increase as the previous digit is more than 5.

Other Methods to Rounding Figure to Two Decimal Places in Python

In addition to the above all methods, there are some other methods of Python. By using these methods, you can round float value to 2 decimal or 3 decimal places. These methods are also simple and useful. However, the methods are not commonly used methods to convert float to 2 decimal places.

Round Float to Two Decimal Places Using format() of Python

To convert float to two decimal places, you have to use the format(). The second argument is the ‘.2f‘ and the first argument is the float variable you have to pass.

myFloat = 23.98765; print(format(myFloat, '.2f'));

print(format(myFloat, '.2f'));

Output

23.99

The above example round the float to two decimal places. However, you can convert float to 3 decimal places also. You have to read further to convert the float to 2 decimal places in Python.

Round Float to Three Decimal Places Using format() of Python

To convert float to two decimal places, you have to pass the third argument as ‘.3f‘. The first argument of the format() should be the float variable which you want to convert.

myFloat = 23.98765; print(format(myFloat, '.3f'));

print(format(myFloat, '.3f'));

Output

23.988

The above example showing the converted float value to two decimal places. In addition to this method, there is also a different method to convert to two decimal places.

Using Different Method to Round Float to Two Decimal Places

You have to use the below-given example to change the float value to two decimal places. This may be the same as the format() but the way of using it is different. You have to use the percentage(%) sign with this method.

myFloat = 23.98765; print("%.2f"%myFloat);

Output

23.99

The above example showing the example to change the float value to two decimal places in Python.

DOWNLOAD Free PYTHON CHEAT SHEET

You may also like to read

I hope you like this tutorial on how to round float to 2 decimal places in Python.

References

How to round to two decimal places in Python

Borislav Hadzhiev

Last updated: Jul 12, 2022

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.