Representing Money at Rocker

As a fintech company, we spend quite a significant amount of our time writing code that models money movement in various ways. What follows is a need for us to represent money in a safe and clear way. The first step is to represent monetary values in our system, the second step would be to operate on these values. Let’s explore a few different ways we could do this.

A naive approach

If you’ve never worked with money before and only operating in your own country, there might be a tendency to think extremely locally and omit the currency. It’s better if we make this explicit so we can support multiple currencies.

Improvement

This solution gets us quite far. There are a few issues with representing money using decimal values. Floating points are usually approximations of numeric values and can cause a lot of issues due to rounding errors. `Double` might lack the precision that we want. Decimal values in general tend to be quite difficult to deal with and we could instead represent money using integers.

When representing monetary values using integers, we set the amount for the currency in its smallest currency unit (also called minor units), e.g. 100 cents for 1 dollar or 1 for ₩1, South Korean Won being a zero-decimal currency. By choosing to do it like this, it makes it easy to represent it in all layers of our program, from API to DB. It’s also easy to operate on, e.g. we would model 3.15 USD + 5.73 USD as

The downside of this approach is that there is no easy way to represent fractional amounts. How do you represent half a cent? There are some solutions to this, one is to use rational values to represent money. However, at Rocker we made the decision that this issue never has come up for us so we rather skip it since it bring a lot of obscurity.

Let’s make the scale explicit so that we do not need to worry about whether it’s minor or major units and that it’s part of the representation rather than just in the name.

This allows us to represent 100 USD either as

Operations

One of the things that we discussed internally was about how to add, subtract, divide, and multiply monetary values of two different currencies and how we can avoid mistakes where we add amounts of two different currencies. Is there a way for us to leverage Scala’s type system somehow for us to be able to make the type explicit?

Type parameters

and then having a method like:

Return type

It’s just annoying having to deal with Either everywhere if you know that you’re only dealing with a specific currency.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store