top | item 41777430

(no title)

hiddew | 1 year ago

How does it compare to the Java money API (https://jcp.org/en/jsr/detail?id=354) and the related Kotlin DSL in https://github.com/hiddewie/money-kotlin/?tab=readme-ov-file...?

discuss

order

stickfigure|1 year ago

I'm surprised nobody has mentioned Joda Money yet:

https://www.joda.org/joda-money/

From the same person that brought us Joda Time (ie, what the java time API was based on). I've used Joda Money a lot and it's great.

Honestly I prefer APIs that look like APIs and I think this trend towards inventing DSLs is a bad one. Rails works because there's a critical mass of people who have adopted what is essentially a whole new language on top of Ruby. A money library doesn't warrant a new language, it's unnecessary cognitive load. This new money library would look fine with simple constructors and method calls.

eriksencosta|1 year ago

Joda is impressive and has great performance.

The examples were written using the infix notation but you can just use regular method calls. For example:

val price = Money.of(100, "USD")

val shipping = Money.of(5, "USD")

val subtotal = price.plus(shipping)

val discount = Percentage.of(10)

val total = subtotal.decreaseBy(discount)

total.allocate(2)

total.allocate(60.percent(), 40.percent())

nogridbag|1 year ago

I personally went with Joda money versus the Java money API mentioned above. Our needs are a bit simpler and the Joda Money API is a bit simpler to understand. Our app only deals in USD so I wrote a small utility class to help initialize Money instances so devs don't have to write:

    Money.of(CurrencyUnit.USD, amount)
...everywhere and do a few other things like total Money instances.