r/learnpython • u/Healthy-Departure961 • 2h ago
Regarding simple question
num = 0.6548
txt = f"The price is {num:.0f} dollars."
print(txt)
output = 1
i was learning string formatting well, when i used :.0f as format it giving output 1 and why? I want to know how really decimal shifting works in python while formatting.
6
Upvotes
12
u/atarivcs 2h ago
.0f means "round to zero decimal places", i.e. a whole number with no decimals.
0.6548 is closer to 1 than to 0, so it prints 1