我在这段代码的输出中得到了很多小数(华氏到摄氏度转换器)。

我的代码目前看起来是这样的:

def main():
    printC(formeln(typeHere()))

def typeHere():
    global Fahrenheit
    try:
        Fahrenheit = int(raw_input("Hi! Enter Fahrenheit value, and get it in Celsius!\n"))
    except ValueError:
        print "\nYour insertion was not a digit!"
        print "We've put your Fahrenheit value to 50!"
        Fahrenheit = 50
    return Fahrenheit

def formeln(c):
    Celsius = (Fahrenheit - 32.00) * 5.00/9.00
    return Celsius

def printC(answer):
    answer = str(answer)
    print "\nYour Celsius value is " + answer + " C.\n"



main()

我的问题是,如何让程序把每个答案四舍五入到小数点后2位?


当前回答

简单的例子

比尔= 10.24 打印(圆(10.241))

其他回答

如果需要避免浮点数舍入问题,可以使用numpy舍入。

你需要安装numpy:

pip install numpy

代码是:

import numpy as np

print(round(2.675, 2))
print(float(np.round(2.675, 2)))

打印

2.67
2.68

如果你用合法四舍五入管理资金,你应该使用这个。

float(str(round(answer, 2)))
float(str(round(0.0556781255, 2)))

你要把答案四舍五入。

round(value, signantdigit)是常见的解决方案,但从数学角度来看,当你要舍入的数字的左边的数字是5时,这有时并不像人们所期望的那样运行。

以下是这种不可预测行为的一些例子:

>>> round(1.0005,3)
1.0
>>> round(2.0005,3)
2.001
>>> round(3.0005,3)
3.001
>>> round(4.0005,3)
4.0
>>> round(1.005,2)
1.0
>>> round(5.005,2)
5.0
>>> round(6.005,2)
6.0
>>> round(7.005,2)
7.0
>>> round(3.005,2)
3.0
>>> round(8.005,2)
8.01

假设您的目的是对科学统计进行传统的舍入,这是一个方便的包装器,可以使舍入函数按预期工作,需要导入诸如Decimal之类的额外内容。

>>> round(0.075,2)

0.07

>>> round(0.075+10**(-2*6),2)

0.08

啊哈!基于这个我们可以做一个函数。

def roundTraditional(val,digits):
   return round(val+10**(-len(str(val))-1), digits)

基本上,这将给字符串添加一个非常小的值,以迫使它在不可预测的实例上正确舍入,而通常情况下,当你期望它使用舍入函数时,它不会正确舍入。一个方便的值是1e-X,其中X是你试图用整数加1计算的数字字符串的长度。

The approach of using 10**(-len(val)-1) was deliberate, as it the largest small number you can add to force the shift, while also ensuring that the value you add never changes the rounding even if the decimal . is missing. I could use just 10**(-len(val)) with a condiditional if (val>1) to subtract 1 more... but it's simpler to just always subtract the 1 as that won't change much the applicable range of decimal numbers this workaround can properly handle. This approach will fail if your values reaches the limits of the type, this will fail, but for nearly the entire range of valid decimal values it should work.

所以完成的代码将是这样的:

def main():
    printC(formeln(typeHere()))

def roundTraditional(val,digits):
    return round(val+10**(-len(str(val))-1))

def typeHere():
    global Fahrenheit
    try:
        Fahrenheit = int(raw_input("Hi! Enter Fahrenheit value, and get it in Celsius!\n"))
    except ValueError:
        print "\nYour insertion was not a digit!"
        print "We've put your Fahrenheit value to 50!"
        Fahrenheit = 50
    return Fahrenheit

def formeln(c):
    Celsius = (Fahrenheit - 32.00) * 5.00/9.00
    return Celsius

def printC(answer):
    answer = str(roundTraditional(answer,2))
    print "\nYour Celsius value is " + answer + " C.\n"

main()

...应该能给你预期的结果。

您也可以使用十进制库来实现这一点,但我建议的包装器更简单,在某些情况下可能是首选。


编辑:感谢blackknight指出,5边缘情况只发生在这里的某些值。

可以使用round函数,它的第一个参数是数字,第二个参数是小数点后的精度。

在你的情况下,它将是:

answer = str(round(answer, 2))

如果你只想输出四舍五入的结果,你可以使用Python 3.6以来引入的f字符串。它的语法与str.format()的格式字符串语法相同,不同之处在于在字面值字符串前面加了一个f,并且将变量直接放在字符串中,放在花括号内。

.2f表示四舍五入到小数点后两位:

number = 3.1415926
print(f"The number rounded to two decimal places is {number:.2f}")

输出:

The number rounded to two decimal places is 3.14