Last Updated on April 24, 2023 by Roshan Parihar
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.
1 2 |
myFloat = 23.98765; print(round(myFloat, 2)); |
Output
The above example shows 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.
1 2 |
myFloat = 23.98765; print(round(myFloat, 3)); |
Output
The above example shows the rounded 3 decimal place output of the float. The third digit after the decimal also gets the increase as the previous digit is more than 5.
Other Methods of Rounding Figures to Two Decimal Places in Python
In addition to the above 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.
1 2 |
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 that you want to convert.
1 2 |
myFloat = 23.98765; print(format(myFloat, '.3f')); |
Output
The above example shows 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 Methods 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.
1 2 |
myFloat = 23.98765; print("%.2f"%myFloat); |
Output
The above example shows the example to change the float value to two decimal places in Python.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read
- How to change a string to uppercase in Python
- Convert all string to lowercase in Python
- Combine two string variables in Python
I hope you like this tutorial on how to round float to 2 decimal places in Python.
References