2 回答

TA貢獻(xiàn)1817條經(jīng)驗 獲得超14個贊
0.2f中"."前面的數(shù)字表示這個數(shù)字要占有多少個字節(jié),“0”表示不刻意規(guī)定占用字節(jié)數(shù),只要能表示這個數(shù)字就行。而“.”后面的數(shù)字表示小數(shù)位數(shù)。
d表示這個數(shù)據(jù)類型是整型,還有s表示數(shù)據(jù)類型是字符串。
以下是我們python教材中的原話,供你參考:
>>> "Hello %s %s, you may have won $%d!" % ("Mr.", "Smith", 10000)
’Hello Mr. Smith, you may have already won $10000!’
>>> ’This int, %5d, was placed in a field of width 5’ % (7)
’This int, 7, was placed in a field of width 5’
>>> ’This int, %10d, was placed in a field of width 10’ % (7)
’This int, 7, was placed in a field of width 10’
>>> ’This float, %10.5f, has width 10 and precision 5.’ % (3.1415926)
’This float, 3.14159, has width 10 and precision 5.’
>>> ’This float, %0.5f, has width 0 and precision 5.’ % (3.1415926)
’This float, 3.14159, has width 0 and precision 5.’
>>> "Compare %f and %0.20f" % (3.14, 3.14)
’Compare 3.140000 and 3.14000000000000012434’

TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊
添加回答
舉報