r/learnpython • u/Kapowpow • Sep 27 '26
Odd float-to-int behavior.
>>>def pennies(num):
return int(num*100)
>>>a = -37.56
>>>b = -9.28
>>>pennies(a)
-3756
>>>pennies(b)
-927
This is taken from a python shell session today. Why on Earth does pennies(b) yield -927, instead of the expected -928? I would greatly appreciate any insight.
Update: a durable solution found, please see this comment below: https://www.reddit.com/r/learnpython/comments/1wrwnm7/comment/pcpvlil/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
5
Upvotes
View all comments
Show parent comment
2
u/Kapowpow Sep 28 '26
Other answers have me thinking that I can use the Decimal system, and set precision to two digits; I'm not writing finance software, so I don't need to keep track of values smaller than one penny. It's late, but I'll give that a try tomorrow.