在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
请注明出处。