首页 > 编程知识 正文

python怎么保留三位小数,python保留两位小数补0

时间:2023-05-06 03:09:44 阅读:167010 作者:1736

在Python中保持小数点后2位的方法如下。

四舍五入,保留两位小数

kxdlr:使用字符串格式a=12.345

打印((%.2f ) %a ) ) ) ) ) )。

12.35

xsdbg:使用round内置函数a=12.345

round(a,2 ) )。

12.35

方法使用:decimal模块from decimal import Decimal

a=12.345

decimal(a ).quantize ) decimal('0.00 ' ) )

电子邮件(12.35 ) )。

只保留小数点后两位,不需要四舍五入

kxdlr:使用数组中的片a=12.345

str(a ).split ('.') [0].'str ).split ) '.' ) [1][:2]

' 12.34 '

xsdbg:使用导入re模块

a=12.345

re.findall(r'd{1,} )? .d{2} ',str(a ) )

['12.34']

有关更多Python相关技术文章,请参考Python教程栏进行学习。

正文地址: http://it byc.com/python/22088.html

请注明出处。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。