在打印语句中进行计算时,如何四舍五入到小数点后 2 位?
temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print((5/9)*(temp - 32))
例如:
>>> print("%.2f" % 16.25667)
16.26
您可以使用round()
函数来实现此目的。它更容易理解。
此函数的第一个参数是浮点结果或等效表达式。
第二个参数是要舍入的位置。
temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print(round((5/9)*(temp - 32),2))
这是工作代码,我对其进行了测试。