情況一:保留兩位小數(shù)台腥,并做四舍五入處理
1烫止、使用字符串格式化
>>> a=12.345
>>> print("%.2f" %a)
12.35
2、使用round內(nèi)置函數(shù)
>>> a=12.345
>>> round(a,2)
12.35
情況二:保留兩位小數(shù)疾棵,無需四舍五入處理
1戈钢、使用序列中切片
>>> a=12.345
>>> str(a).split('.')[0]+'.'+str(a).split('.')[1][0:2]
'12.34'
2、使用re模塊
>>> a=12.345
>>> re.match(r'\d+.\d{2}',str(a))
<re.Match object; span=(0, 5), match='12.34'>