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
wiseowise#
Infix functions is one of the worst features of Kotlin.
serial_dev#
Congrats on the library, I’ll be looking into it for some nuggets of knowledge.

However, the Kotlin code looks absolutely… wrong? Strange? It just feels like a lot of magic to end up with ugly code.
This is all subjective, so my question is only this: is this how Kotlin is like in 2024?

afh1#
>However, no mainstream language has a first-class data type for representing money

Parcal has a Currency type. Though, I can understand not calling it mainstream... Fun fact it's a fixed point type which is just a Int64 behind the scenes, at least in Delphi.

yafetn1 reply#
The currency codes could probably be inline value classes. That way, you can do

    val price = 100 money USD
Note the lack of quotes around USD.
sigh_again2 replies#
Maintaining an up to date currency list is, quite frankly, hell. Your code will always, always be more up to date than said list.
9467899876491 reply#
You could argue the same for timezones in a date library, yet they have them. I would think a library dedicated to money will in fact be the most up to date.
sigh_again#
Do they ? I've never once had a library that stores Europe_Paris, or Offset_Plus_7_45, they've always been stringly typed. Do you have an example of who'd be crazy enough to maintain a wrapper around tzdb?
Tainnor1 reply#
This could be mitigated by making the currency interface / abstract class open to extension by the user. The common currencies would be provided by the library and any additional ones could be defined by the user.
sigh_again1 reply#
Making currencies a concrete implementation is a terrible idea. There is no benefit to it at all, except throwing OOP into something that doesn't need it. A single Money class covers all needed cases, the difference between USD and BTC is... everything. Different smallest denominations, different formats, different everything. You don't even need concrete implementations

    private data class Money(val name: String, val amount: Int) {
         operator fun  plus(other: Money): Money {
             return Money(name, amount + other.amount)
         }

         fun  plus(other: Money, converter: (Money) -> Money): Money {
              return converter(other) + this
         }
    }

    private object EUR
    private object USD

    val eur = Money("EUR", 10)
    val usd = Money("USD", 20)

    usd + eur // error
This gives you entirely user defined currencies, does not pollute the global scope with unneeded currencies, allows you to plug in any conversion technique (pop off, make a network call), and fails if you try to add USD and EUR without converting one into the other.

Currencies should always be the user's responsibility to provide.

Continue this thread — 1 reply

Load more comments