I thought if I made the numbers float that it would give me a number with a decimal.
height = 65.0 / 12.0
print height
What I get back is 5 without the remainder when I use it in a string. Like say:
print "He is %d tall." % height
I thought if I made the numbers float that it would give me a number with a decimal.
height = 65.0 / 12.0
print height
What I get back is 5 without the remainder when I use it in a string. Like say:
print "He is %d tall." % height
If you want to stick to the %-formatting use %g
to get the decimal places:
In [5]: print("He is %g tall." % height)
He is 5.41667 tall.
You could also define the number of decimal places (e.g. 2 places) like this %.2f
:
In [13]: print("He is %.2f tall." % height)
He is 5.42 tall.
The pythonic way to format, however, would be:
In [14]: print("He is {0:.2f} tall.".format(height))
He is 5.42 tall.
Here you can find a nice overview: http://www.python-course.eu/python3_formatted_output.php
EDIT:
cricket_007 is right: OP is using Python2. Thus the correct syntax would be:
print "He is {0:.2f} tall.".format(height)