Lurker News
Manipulating monetary amounts is a common computing chore. However, no mainstream language has a first-class data type for representing money, it’s up to programmers to code abstractions for it. This isn’t an issue per se until dealing with rounding issues from operations like installment payments (e.g., buy now, pay later), foreign exchange, or even simple things like fee processing and tax collection.

Inspired by my days at N26 Brasil dealing with these challenges, I introduce Money: a Kotlin library that makes monetary calculations and allocations easy.

Comments (228) 50 threads

Earlier comments
mplewis2 replies#
Banks typically use a fixed floating point of 1 integer step = 1/100 cent. Is this value configurable in your library?
pasc18781 reply#
I doubt Japanese ones do.
jpollock#
Yes, because partial units still have meaning when dealing with interest and taxation.
vintagedave1 reply#
Delphi has a currency type, as does C++Builder (so, a native Currency type available in C++ with that toolchain.)

This one seems to carry units, as in, it is not a type with math for a known amount of fixed point precision that differentiates this library, but that it captures that plus the unit -- the currency, USD, EUR, etc -- that it is in.

eriksencosta#
You are right. Languages indeed have support for currencies and decimal numbers. My implementation is based on Fowler's Money pattern. The Money object is thus a value object representing a monetary amount in a given currency.
pbreit2 replies#
Are integers still a preferred method for working with and storing monetary amounts or can we go back to decimals now that most languages handle decimals decently? Monetary amounts pretty much always need to eventually be presented to humans so handling them as integers is quite a pain.
hansonkd#
A decimal type is sometimes orders of magnitude slower than integer do simple stuff like summing (at least in Postgres and Python).

This comes into play if you have to do aggregate functions across tens of thousands of rows and can kill performance compared to integers.

jacobsenscott#
It isn't that common to need fractions though. Money is typically handled in integer amounts (we just divide the amounts by 100 or whatever to make it easier for humans to grok). Nobody's going to pay half a cent for a latte for example. If for some reason you do need to deal with 1/1000 of a dollar, or 1/10000 of a dollar, you can still use integers. You just need to know the scaling factor.

Your presentation layer is probably always going to need to do some manipulation of the number anyway (put a dollar sign on it, etc) so using a decimal amount just for presentation needs isn't a huge win. But it will work too.

atemerev2 replies#
Cool! As underlying values, do you use integers, bigdecimals, or a decimalized double hack like in OpenHFT?
eriksencosta1 reply#
It uses BigDecimal. My first goal with the library was to provide a well-designed API. So to keep it simple for myself, I relied on BigDecimal for the calculations.
atemerev#
I think you are right, API is the most important part; internal implementation can be optimized later.
vamega2 replies#
What is the double hack used in OpenHFT? I tried looking this up and came up short.
pcl#
I’ll byte. Perhaps you didn’t look long enough. I’m sure the answer will float by at some point.

Load more comments