【问题标题】:How to display to the 2nd decimal in python? [duplicate]如何在python中显示到小数点后第二位? [复制]
【发布时间】:2019-08-25 16:29:09
【问题描述】:

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

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

【问题讨论】:

    标签: python


    【解决方案1】:

    例如:

    >>> print("%.2f" % 16.25667)
    16.26
    

    【讨论】:

      【解决方案2】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-14
        相关资源
        最近更新 更多