我在这段代码的输出中得到了很多小数(华氏到摄氏的转换器)
我的代码当前如下所示:
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()
所以我的问题是,如何让程序将每个答案四舍五入到小数点后第二位
使用^{} 的syntax以两个小数位显示
answer
(不改变answer
的基本值):其中:
:
介绍了format spec0
为数字类型启用符号感知零填充.2
将precision设置为2
f
将数字显示为定点数字可以使用^{} 函数,该函数的第一个参数是数字,第二个参数是小数点后的精度
在您的情况下,它将是:
大多数答案建议
round
或format
round
有时会向上取整,在我的例子中,我需要向下取整变量的值,而不仅仅是这样显示我在这里找到了答案:How do I round a floating point number up to a certain decimal place?
或:
相关问题 更多 >
编程相关推荐