Home  >  Article  >  Backend Development  >  How to keep one decimal place in python

How to keep one decimal place in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-02 09:21:0125277browse

How to keep one decimal place in python

Method 1

Use round function

For example:

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

Related recommendations: "Python Video Tutorial"

Method 2

"%.1f"%a

For example :

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

Method 3

First introduce the decima function

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

Output result:

134.5

The above is the detailed content of How to keep one decimal place in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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