如何在python中显示到小数点后第二位?

时间:2019-08-25 16:29:09

标签: python

在打印语句中进行计算时,如何四舍五入到小数点后第二位?

temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print((5/9)*(temp - 32))

2 个答案:

答案 0 :(得分:1)

例如:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.12.1</version>
</dependency>

答案 1 :(得分:1)

您可以为此使用round()函数。更容易理解。

此函数的第一个参数是浮点结果或等效表达式。

第二个参数是要四舍五入到的位置。

temp = float(input("Input temperature in Fahrenheit to be converted to Celcius: "))
print(round((5/9)*(temp - 32),2))

这是工作代码,我已经对其进行了测试。