For quite a few years, I was in charge of developing billing and payment systems. Luckily (or unluckily) for me, I had to build many of those systems from scratch without much guidance from payment experts. I’ve made a ton of mistakes but also fixed most of them. Some of the mistakes were deep in the app’s core, which made them so hard to fix that it was better to do a re-write than trying to fix them. That wasn’t fun either. If you want to spare yourself some pain when building a payment system— follow these tips below!

A Moment for Terminology

Minor and major units — The fractional part of a currency. Also called a subunit. For example, for the US currency, USD, the cents are the minor units, and the dollars are the major units.

Lesson 1. Avoid Using a Decimal Point to Represent Money in Your Back-End Logic

So currencies have 100 minor units in each major unit, does that mean you should use a decimal point to represent this information in memory, databases, and calculations? It is tempting, but you should absolutely not do that. Here are two reasons:

1. Representing money as floating-point will make you lose money

This might come as a surprise, but the floating-point type can’t accurately represent decimals. Computers only “understand” binary, and under current 32/64 bit binary representation, the only numbers between 0.0 and 1.0 that can be represented accurately are 0, 0.25, 0.5, 0.75, and 1.0. Computers just can’t represent fractions properly, so they approximate: Although 1.999997 might seem like a very close number to 2, if you multiply, add, and subtract it enough times, you’ll be missing some money and gain many headaches.

2. Currencies don’t always have 100 minor units per major

Assuming you can use decimals is rooted in the misconception that all currencies have 10/100/1,000 minor units. Well, some currencies, like the Japanese yen, don’t have minor units at all, and others are even non-decimal. This serves as another hint that decimal isn’t the right representation for money.

#software-development #startup #distributed-systems #payments #deep-learning

6 Hard-Learned Lessons From Building Payment Systems
1.25 GEEK