首页 > 解决方案 > 格式化总数,使其不显示小数位

问题描述

所以,我试图让这段代码最后(总计)不显示任何小数位......但我真的找不到办法做到这一点。有没有办法做到这一点而不必重写一切?


test1_weight = float(input("Type your 1st Test weight: "))

test2_grade = float(input("Type your 1st Test grade: "))

test2_weight = float(input("Type your 1st Test weight: "))

total = (test1_grade * test1_weight + test2_grade * test2_weight)/(test1_weight + test2_weight)

print ("The weighted average is: ", total)

标签: python-3.x

解决方案


您可以total转换为整数:

total = int(total)

例如:

total = 3.75
total = int(total)
print(total) # 3

推荐阅读