# 如何用Python舍入到2位小数？

``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()` `

` `answer = str(round(answer, 2))` `

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

• `:`介绍格式规范
• `0`为数字types启用标志感知零填充
• `.2`将精度设置为`2`
• `f`将数字显示为一个定点数字
` `float(str(round(answer, 2))) float(str(round(0.0556781255, 2)))` `

`round`总是圆了

` `round(2.357, 2) # -> 2.36` `

` `import math v = 2.357 print(math.ceil(v*100)/100) # -> 2.36 print(math.floor(v*100)/100) # -> 2.35` `

` `from math import floor def roundDown(n, d=8): d = int('1' + ('0' * d)) return floor(n * d) / d` `

` `def printC(answer): print "\nYour Celsius value is %.2f C.\n" % answer` `

` `def typeHere(): 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(Fahrenheit): Celsius = (Fahrenheit - 32.0) * 5.0/9.0 return Celsius def printC(answer): print "\nYour Celsius value is %.2f C.\n" % answer def main(): printC(formeln(typeHere())) main()` `

` `round(80.23456, 3)` `

` `answer = str(round(answer, 2))` `

`round(value,significantDigit)`是这样做的普通解决scheme，但是当数字直接位于四舍五入的数字的下方（数字的左侧）有一个`5`位数字时， 有时不会像math视angular那样运行。

` `>>> 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` `

` `>>> 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))` `

` `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()` `

…应该给你你期望的结果。