練習:
輸出你的身體指標:
身高:170
體重50.5
BMI指數:50.5/(170+50.5)
從上面可以看出,BMI指數是fload類型,如果需要保留兩位小數,有兩種寫法
第一種寫法,使用round()函數
#輸出你的身體指標 height=170 weight=50.5 bmi=weight/(height+weight) print('您的身高是:'+str(height)) print('您的體重是:'+str(weight)) print('您的BMI指數是:'+str(round(bmi,2)))
執行結果:
第二種寫法,使用format函數,代碼如下:
#輸出你的身體指標 height=170 weight=50.5 bmi=weight/(height+weight) print('您的身高是:'+str(height)) print('您的體重是:'+str(weight)) print('您的BMI指數是:'+'{:0.2f}'.format(bmi))
執行結果:
綜上所述,round函數和format函數都可以對float類型的數據進行截取