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

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

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位?


当前回答

你要把答案四舍五入。

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边缘情况只发生在这里的某些值。

其他回答

因为你想要你的答案在十进制数字,所以你不需要类型转换你的答案变量str在printC()函数。

然后使用printf风格的字符串格式

使用str.format()的语法显示两个小数点后的答案(不改变答案的底层值):

def printC(answer):
    print("\nYour Celsius value is {:0.2f}ºC.\n".format(answer))

地点:

:介绍格式规范 0为数字类型启用可识别符号的零填充 .2将精度设置为2 F将该数字显示为一个定点数

如果你不仅需要四舍五入的结果,而且还需要用四舍五入的结果进行数学运算,那么你可以使用十进制。十进制https://docs.python.org/2/library/decimal.html

from decimal import Decimal, ROUND_DOWN

Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32') 
from decimal import Decimal, ROUND_HALF_UP

# Here are all your options for rounding:
# This one offers the most out of the box control
# ROUND_05UP       ROUND_DOWN       ROUND_HALF_DOWN  ROUND_HALF_UP
# ROUND_CEILING    ROUND_FLOOR      ROUND_HALF_EVEN  ROUND_UP

our_value = Decimal(16.0/7)
output = Decimal(our_value.quantize(Decimal('.01'), 
rounding=ROUND_HALF_UP))
print output

到目前为止我发现的最简单的解决方案,不知道为什么人们不使用它。

# Make sure the number is a float
a = 2324.55555
# Round it according to your needs
# dPoints is the decimals after the point
dPoints = 2
# this will round the float to 2 digits
a = a.__round__(dPoints)
if len(str(a).split(".")[1]) < dPoints:
    # But it will only keep one 0 if there is nothing,
    # So we add the extra 0s we need
    print(str(a)+("0"*(dPoints-1)))
else:
    print(a)