Your balance is $0.30000000004

Common problems found in money-handling software

If you write software that happens to manage money, I feel you. As an industry, we too often don’t hold ourselves to very high standards regarding reliability of the things we build. While an elevator is practically incapable of failing, things going wrong in software is business as usual. As a result, software that manages money if often built on flaky foundations.

In this article we attempt to build an inventory of frequent issues found in financial software, an inventory that you can use to check your current system against.

Sweetest spot on earth in the center — will rent

Arithmetical errors

Floating point

Let’s start with the obvious: the first problem that I perhaps encounter the most is the use of floating point numbers for the manipulation and storage of money amounts. Why is that a problem? Well, if you open a javascript console and execute the following:

"Your balance is $" + (0.1 + 0.2)

You’ll get the title of this article.

Because, by definition, we cannot have an exact representation of 0.1 or any negative power of ten in binary, we cannot use floats to accurately store and operate on money values without also having to use rounding.

The simplest way to completely avoid this issue is to use another data type than floats for representing monetary amounts. A frequent solution is to use integers, representing a money amount in its smallest unit (for example, 100 cents for USD instead of 1.00 USD). Decimal data types also work well, granted that your language supports them (java has BigDecimal, javascript doesn’t have any).

Allocation

The second most-encountered arithmetical problem probably is the use of division & rounding instead of allocation. Take this example: your ride-hailing app has a small reservation fee of $0.99 that you equally split between you and the driver. Should you use division and rounding,

= round(0.99 / 2) + round(0.99 /2)

= round(0.495) + round(0.495)

= 0.5 + 0.5

= 1

Assuming that your rounding function rounds to the nearest integer half up, a common default , an extra cent will be introduced to the system as $0.99 becomes $1. Rinse and repeat this operation on each ride and your system will quickly be completely off.

For this problem to be solved, rounding needs to be completely taken off the table and an actual decision needs to replace it: will the driver of the platform get the unsplittable cent?

👉 This solution is simply called allocation, and is often implemented by money manipulating libraries.