2 decimal places instead of one, but code is not requiring a float

181 Views Asked by At

I'm trying to understand how to get 2 decimal places in this code. Codio is reading it as one. It is not requiring a float command. If I were to enter a float, where would I actually add the $%.2f command in regard to this string. I am very new to coding, so I just want to make sure that I am creating the proper syntax. Been working on this for hours.

if rentalCode == "B":
    budgetCharge = 40.00
    baseCharge = rentalPeriod * budgetCharge

elif rentalCode == "D":
   budgetCharge = 60.00
   baseCharge = rentalPeriod * budgetCharge

elif rentalCode == "W":
   budgetCharge = 190.00
   baseCharge = rentalPeriod * budgetCharge

print(baseCharge) 
1

There are 1 best solutions below

0
On

Defining budgetCharge with 2 decimal places will have no use. You can specify how many decimal places by using format strings:

# old style
print('%.2f' % baseCharge)

# new style
print('{:.2f}'.format(baseCharge))