Dolcens Dolcens - 2 years ago 142
Python Question

Python - how to round down to 2 decimals

I am getting a lot of decimals in the output of this code (Fahrenheit to Celsius converter).

My code currently looks like this:

def main():

def typeHere():
global Fahrenheit
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"


So my question is, how to I successfully make the program round down every answer to 2 decimals?

Answer Source

You can use the round function, which takes as first argument the number and the second argument is the precision.

In your case, it would be:

answer = str(round(answer, 2))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download