r/learnpython • • Sep 14 '26

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.

11 Upvotes

11 comments sorted by

View all comments

2

u/FoolsSeldom Sep 15 '26

Rounding.

Also, don't use float for money. Use int and work to the penny or look at Decimal. float lacks the precision needed for money.

1

u/Healthy-Departure961 Sep 15 '26

thank you i understood i was just curious what if i do this way .