2019-12-15

Python 學習筆記-1

解答 小數點位數(format函數)問題:
h=160
w=55.22
print('My height is {:10.2f}'.format(h) + ' CM.\nMy weight is {:10.2f}'.format(w)+' KG.')
輸出得到
My height is     160.00 CM.
My weight is      55.22 KG.
結束。
解說:指定變數h為整數160,指定變數w為浮點數(float)55.22。
輸出其值,使用format函數,{:10.2f}表示數字位數寬度10位並保留到小數點後兩位。