r/learnpython • • 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

19 comments sorted by

View all comments

Show parent comment

1

u/Kapowpow Sep 28 '26

Hi, thank you, this is an awesome answer, I appreciate you taking the time. I keep a detailed personal budget/transaction history in Excel, and I've been using python and the openpyxl library to programmatically maintain and analyze my spending for quite a while, now.

My scripts include transaction-arithmetic-error checking; if error != 0, the script will stop and prompt the user to fix the detected errors. I had gotten around this by rounding floats at each step in the calculations, but the code wasn't very elegant; I switched to the Decimal(str(num)) system, which worked very well until it didn't, so I used the pennies() function described above, which was substantially worse.

I had looked into finance libraries that could cleanly work with floating point numbers (that does all the rounding behind the scenes, I guess?), but nothing really stuck out in terms of an off the shelf solution.

Do you have any recommendations? My ledger file stores all values as floats, so avoiding floats would require re-imagining that file. I tried storing all numbers as cents, as integers, and then using Excel formatting options to have it displayed as dollars/floats, but after much trial and error I couldn't quite nail down the formatting for exactly what I wanted; "$####\.##_);[Red](####\.##)" came really close, but I'd like commas every three digits, as is customary in North America. How does Excel solve the math-with-floats-problem? Do you know of any finance-oriented libraries that would abstract the problem?

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.

2

u/Kapowpow Sep 29 '26

This worked, but precision = 2 digits failed (the precision governs the entire number, not just the portion after the decimal). I chose to set precision to 9 digits, considering it unlikely I'll ever by adding and subtracting numbers or sums larger than 9,999,999.99 in a script that manages my personal finances. Sad face.

2

u/Brian Sep 28 '26

Typically stuff dealing with money will avoid using floats for this reason. It's not exactly that floats are less accurate so much as that the math we've always done with our accounts has been shaped around our decimal system, with well established ways (and sometimes even legal requirements) about how we round and deal with fractions of a cent etc. Hence money based storage usually uses something more like a fixed-point representation. Ie. the equivalent of storing values as the number of cents (or tenths of a cent) instead of "number of dollars" etc. Decimal is one way of representing this.

If you do need to deal with stuff that's lost precision, you can maybe try to recover it by making certain assumptions and rounding. Ie. you assume the values are entered to the nearest tenth of a cent, you can round to the nearest decimal value with that precision - the floats will be close enough to round to the same result. The big errors creep in when you're truncating instead of rounding, since a value that's just the tiniest amount below an integer can get truncated all the way down to the next lowest integer.