Home > Backend Development > Python Tutorial > How to retain one decimal place in python output

How to retain one decimal place in python output

(*-*)浩
Release: 2019-07-09 10:26:09
Original
49498 people have browsed it

Today I will share with you an example of correctly retaining one decimal place in Python. It has a good reference value and I hope it will be helpful to everyone.

How to retain one decimal place in python output

Method 1, use round function (recommended learning: Python video tutorial)

For example:

a=12.34567889
round(a,1)=12.3 保留一位小数
round(a,2)=12.35 保留二位小数
Copy after login

Method 2,

For example:

a=12.34567889
print(“%.1f”%a) 保留一位小数
print(“%.3f”%a) 保留三位小数
print(“%.4f”%a) 保留四位小数
Copy after login

Method 3,

is introduced first decima function

from decimal import Decimal
a=134.5657768
t=Decimal(“134.5657768”).quantize(Decimal(“0.0”))
print(t)
Copy after login

Output result:

134.5
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to retain one decimal place in python output. 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