Search code examples
pythonpowershellfloating-point-conversion

Why no floating output?


Just started to learn Python. Why when I run this program in PowerShell the outcome is without floating numbers on line 9 and 10?

height = 74.0 #inches
weight = 180.0 #lbs

#converts from inches to cm/ and from lbs to kg.
cm = height * 2.5400    
kg = weight * 0.453592

print ""
print "Height %d inches in cm is %d " % (height, cm)    
print "Weight in %d lbs in kg is %d " % (weight, kg)

print type(kg)
print type(cm)

Solution

  • You used %d, which is for printing integers. It's converting the floats to integers.

    I would recommend using the format method instead of %:

    print 'Height {} inches in cm is {}'.format(height, cm)
    

    though it's simple enough to do it right with %:

    print "Height %s inches in cm is %s" % (height, cm)
    

    %s calls str on the arguments.