【发布时间】:2019-08-25 16:29:09
【问题描述】:
在打印语句中进行计算时如何四舍五入到小数点后第二位?
temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print((5/9)*(temp - 32))
【问题讨论】:
标签: python
在打印语句中进行计算时如何四舍五入到小数点后第二位?
temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print((5/9)*(temp - 32))
【问题讨论】:
标签: python
例如:
>>> 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))
这是工作代码,我对其进行了测试。
【讨论】: