新聞中心
python保留指定小數(shù)點(diǎn)位數(shù)的方法:

創(chuàng)新互聯(lián)專注于澤庫(kù)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供澤庫(kù)營(yíng)銷型網(wǎng)站建設(shè),澤庫(kù)網(wǎng)站制作、澤庫(kù)網(wǎng)頁(yè)設(shè)計(jì)、澤庫(kù)網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造澤庫(kù)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供澤庫(kù)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1、使用’%.2f’ %f 方法
??f = 1.23456
print('%.4f' % f)
print('%.3f' % f)
print('%.2f' % f)結(jié)果:
1.2346 1.235 1.23
這個(gè)方法會(huì)進(jìn)行四舍五入
2、format函數(shù)
Python2.6 開始,新增了一種格式化字符串的函數(shù) str.format(),它增強(qiáng)了字符串格式化的功能。
基本語法是通過 {} 和 : 來代替以前的 % 。
print(format(1.23456, '.2f')) print(format(1.23456, '.3f')) print(format(1.23456, '.4f'))
輸出結(jié)果:
1.23 1.235 1.2346
3、round()函數(shù)
round() 方法返回浮點(diǎn)數(shù)x的四舍五入值。
以下是 round() 方法的語法:
round( x [, n] )
參數(shù):
x -- 數(shù)值表達(dá)式。
n -- 數(shù)值表達(dá)式。
a = 1.23456 b = 2.355 c = 3.5 d = 2.5 print(round(a, 3)) print(round(b, 2)) print(round(c)) print(round(d))
輸出結(jié)果:
1.235 # 1.23456最終向前進(jìn)位了 2.35 # 2.355居然沒進(jìn)位 4 # 最終3.5居然變?yōu)?了 2 # 最終2.5取值變?yōu)?
網(wǎng)頁(yè)名稱:創(chuàng)新互聯(lián)Python教程:python如何保留指定小數(shù)點(diǎn)位數(shù)?
URL標(biāo)題:http://www.dlmjj.cn/article/djopihj.html
其他資訊
- 數(shù)據(jù)Redis持續(xù)海量數(shù)據(jù)支撐每秒超過1344萬條數(shù)據(jù)(redis每秒多少條)
- 清理Redis集群緩存打造高效運(yùn)行環(huán)境(清理redis集群緩存)
- 現(xiàn)實(shí)版黑客軍團(tuán):ProtonMail遭遇DDOS攻擊
- 如何解決表格復(fù)制無法粘貼到數(shù)據(jù)庫(kù)的問題(表格復(fù)制不能粘貼數(shù)據(jù)庫(kù))
- Python操作系統(tǒng),輕松實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢(python查詢數(shù)據(jù)庫(kù)操作系統(tǒng))


咨詢
建站咨詢
