后浪云Python教程:python怎么设置小数点后保留两位小数点

python中设置保留两位小数点的方法:
1、使用字符串格式化
a = 12.345
print("%.2f" % a)
# 12.352、使用round内置函数
a = 12.345 a1 = round(a, 2) print(a1) # 12.35
3、使用decimal模块
from decimal import Decimal
a = 12.345
Decimal(a).quantize(Decimal("0.00"))
Decimal('12.35')4、使用序列中切片
a = 12.345
str(a).split('.')[0] + '.' + str(a).split('.')[1][:2]
'12.34'5、使用re模块
import re
a = 12.345
re.findall(r"\d{1,}?\.\d{2}", str(a))
['12.34']版权声明:
作者:后浪云
链接:https://www.idc.net/help/182635/
文章版权归作者所有,未经允许请勿转载。
THE END

