If you want it to be .5, change your function to the following
def cm(centimeter):
"""Centimeter to Meters Converter!"""
result = round(centimeter / 100., 1)
print ("%d centimeters is the same as %f meters." % (centimeter, result))
print (result)
Your issue was that you were using the ndigits
parameter of the built-in function round
incorrectly. ndigits
is that second argument you were passing in which was -1. In built-in round
, values are rounded to the closest multiple of 10 to the power minus ndigits. This meant it was rounding it to the closest multiple of 10^1 in your code.
51 / 100.
in Python gives you 0.51 so when you round 0.51 to the closest multiple of 10^1, you obviously were getting 0.0.
Another issue is that you were trying to print your number of meters as a signed decimal value using the string formatting conversion %d
. If you want to see the exact decimal value of result
, you need to use the option for floating point decimal format which is %f
. For more information on this stuff, check out the string formatting page in the docs.