新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何保留指定小數(shù)點(diǎn)位數(shù)?
python保留指定小數(shù)點(diǎn)位數(shù)的方法:

創(chuàng)新互聯(lián)專注于澤庫網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供澤庫營銷型網(wǎng)站建設(shè),澤庫網(wǎng)站制作、澤庫網(wǎng)頁設(shè)計(jì)、澤庫網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造澤庫網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供澤庫網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1、使用’%.2f’ %f 方法
??f = 1.23456
print('%.4f' % f)
print('%.3f' % f)
print('%.2f' % f)結(jié)果:
1.2346 1.235 1.23
這個方法會進(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)?
本文題目:創(chuàng)新互聯(lián)Python教程:python如何保留指定小數(shù)點(diǎn)位數(shù)?
標(biāo)題鏈接:http://m.fisionsoft.com.cn/article/djopihj.html


咨詢
建站咨詢
