新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么設(shè)置小數(shù)點后保留兩位小數(shù)點
python中設(shè)置保留兩位小數(shù)點的方法:

成都創(chuàng)新互聯(lián)公司是專業(yè)的霍城網(wǎng)站建設(shè)公司,霍城接單;提供網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行霍城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
1、使用字符串格式化
a = 12.345
print("%.2f" % a)
# 12.352、使用round內(nèi)置函數(shù)
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'] 網(wǎng)頁標(biāo)題:創(chuàng)新互聯(lián)Python教程:python怎么設(shè)置小數(shù)點后保留兩位小數(shù)點
轉(zhuǎn)載注明:http://fisionsoft.com.cn/article/djoidso.html


咨詢
建站咨詢
