Avoid Negative Division Pitfalls in Python
While working on automated accounting transactions with Beancount, I ran into an unexpected issue involving floating-point number conversions. In financial calculations, it's common practice to convert floating-point values to integers (for example, converting 1.55 to 155) by multiplying by 100, perform the necessary calculations, and then convert the result back by dividing by 100. This method helps maintain precision when dealing with currency values.
Sounds straightforward, doesn't it? That's what I thought until negative numbers entered the equation.
To my surprise, when I attempted to convert -155 back to -1.55, Python returned -2.45 instead. Let's dive into why this happens.