你要把答案四舍五入。
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边缘情况只发生在这里的某些值。