Show
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 PythonTo 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));
Output 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 PythonIf 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));
Output 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 PythonIn 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'));
Output 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'));
Output 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 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 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 The round function takes the following 2 parameters:
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:
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:
Round up float to 2 decimals in Python #To round up a float 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:
|