Explanation of print("%.2f" % total)


#1

the code is like that :smiley:

meal = 44.50
tax = 0.0675
tip = 0.15

meal = meal + meal * tax
total = meal + meal * tip

print("%.2f" % total)
why we added "%.2f" % to the total?


#2

In the future, make sure to mark your code as preformatted text.

Formatting specification is described here:
https://docs.python.org/2/library/stdtypes.html#string-formatting
look for mentions of '.' and 'f' if you just want that specific case described.


#3

I've read the link you specified here still unable to understand this "%.2f"


#4

The % is some kind of placeholder and the f stands for "float"
What the %.2f does, is to fill this place with total, but only 2 numbers after the comma (for example: 22,45).
At the end line, the % total tells the editor, that the placeholder will be filled with total.

So print("%.2f") % total will print the total with 2 numbers after the comma.


#5

Special characters always throw me off.

Thanks frozen!


#6

You're welcome, happy to help anytime :smiley: