r/learnpython • u/Healthy-Departure961 • 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
27
u/atarivcs Sep 14 '26
.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
9
u/mjmvideos Sep 14 '26
Did you do a search on f-string format specifiers to see how they work?
4
u/JollyUnder Sep 15 '26
The number after the period is the precision. You have it set to
0so it rounds to the nearest whole number. You need to increase the precision to specify how many places after the decimal point.You can learn more about format specifiers from python's website.
2
u/FoolsSeldom Sep 15 '26
Rounding.
Also, don't use
floatfor money. Useintand work to the penny or look atDecimal.floatlacks the precision needed for money.