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
ppeetteerr1 reply#
What is up with that API? `1.25.percent()`? `1 money 'USD'`? Love the spirit of a money library, but this is a little odd.
foooorsyth1 reply#
Even as someone familiar with Kotlin, it's hard to decipher what this library is doing without syntax highlighting here. The author is using infix functions and custom extension functions on primitive types. Non-Kotlin-natives will scratch heads.
ppeetteerr#
Oh no, I know how it's done, I'm just surprised by it. There are more straight forward ways of expressing the same thing without using infix functions and those extensions (e.g. `val price = USD(100)` or `val price = Money(100, 'USD')`).
pmarreck#
This covered the API nicely but said nothing about how the amounts were internally-modeled and how rounding was dealt with. It was only mentioned.

I had an idea for a fixed-decimal class (that would also be useful for a money class) that held all inflight amounts as fractions and only did the division and float rounding when you needed to view the amount.

rebeccaskinner2 replies#
Neat. It seems to me like this is filling three separate gaps:

  - fixed point arithmetic
  - tagging values with units
  - localization for parsing currency names into units
I'm curious if Kotlin's type system is expressive enough to allow you to catch at compile time cases where you might be trying to, e.g. add USD to GBP.
ackfoobar#
> catch at compile time

Not in this library, but I think it's possible to put the currency as a type parameter.

xyst#
I wonder if this would handle small decimals.

Edge case?
I want to calculate the installments of 265 Wei (0.000000000000000265 ETH) over a period of 144 months

Load more comments