Home > Article > Backend Development > How to retain decimal places in Python
How to retain the number of decimal places in python: first create a new py file, enter [a=('%.2f' % a)] to retain 2 decimal places; then if you enter [a=('%. 4f' % a)], to retain 4 decimal places; finally, you can also enter [a=format(a, '.2f')] to retain the number of decimal places.
The operating environment of this tutorial: windows7 system, pycharm2020 version, DELL G3 computer.
How to retain the number of decimal points in Python:
The first step is to open pycharm, create a new py file, and enter the
a=1.2222345 a=('%.2f' % a) print(a)
code, as shown below As shown:
The second step, after running the py file, you can see that the decimal point of a retains 2 decimal places, as shown in the following figure:
The third step, if you enter a=('%.4f' % a)
, keep 4 decimal places, please note that it is rounded, as shown in the figure below :
The fourth step, we can also enter a=format(a, '.2f')
to retain the number of decimal points, as shown in the figure below Display:
The fifth step is to run the py file. You can see that 2 decimal places are retained and rounded as well, as shown in the following figure:
Related free learning recommendations: python video tutorial
The above is the detailed content of How to retain decimal places in Python. For more information, please follow other related articles on the PHP Chinese website!