![图片[1]-python怎么设置小数点后保留两位小数点-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ebb53d972da1607.jpg)
python中设置保留两位小数点的方法:
1、使用字符串格式化
a=12.345
print("%.2f"%a)
#12.35
2、使用round内置函数
a=12.345 a1=round(a,2) print(a1) #12.35
3、使用decimal模块
fromdecimalimportDecimal
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模块
importre
a=12.345
re.findall(r"\d{1,}?\.\d{2}",str(a))
['12.34']原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















































暂无评论内容