How to retain decimal places in Python

coldplay.xixi
Release: 2023-01-03 09:27:00
Original
52059 people have browsed it

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.

How to retain decimal places in Python

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)
Copy after login

code, as shown below As shown:

How to retain decimal places in Python

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:

How to retain decimal places in Python

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 :

How to retain decimal places in Python

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:

How to retain decimal places in Python

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:

How to retain decimal places in Python

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!