Show HN: Kotlin Money (opens in a new tab)blog.eriksen.com.br
427 pointsManipulating 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[0] https://en.wikipedia.org/wiki/Cash_rounding
Canada has these guidelines: https://www.canada.ca/en/revenue-agency/programs/about-canad...
So rounding for cash is a different problem that rounding money in general.
Continue this thread — 4 replies
Check for more: https://github.com/eriksencosta/money/blob/trunk/docs/usage/...
There's an example for the Swiss Franc in the README (the code is here [1]).
[0] https://github.com/sernamar/dinero
[1] https://github.com/sernamar/dinero/blob/main/src/dinero/roun...
Continue this thread — 4 replies
Continue this thread — 2 replies
Continue this thread — 1 reply
Equally the allocation trick is good, but you’ll find that the people who really care about this stuff really care about the exact algorithm you’re using and who it favours.
Anyway, the allocation may be tweaked to favour where the additional/missing pennies will be allocated. You can find more at: https://github.com/eriksencosta/money/blob/trunk/docs/usage/...
[0] https://cs-syd.eu/posts/2022-08-22-how-to-deal-with-money-in...
[1] https://www.reddit.com/r/java/comments/wmqv3q/standards_for_...
[1] https://jcp.org/en/jsr/detail?id=354 [2] https://javamoney.github.io/
Money ask = 2.1M USD;
What I don't see with the Java JSR is how exchange rate services are integrated (if at all?). In the best of worlds, as with manifold's units I could write.
Money deposit = 300 USD + 500 EUR;
Where internally the deposit automatically maintains the amounts separately and uses exchange rates only for spot display/calc, transfer, etc.
1. https://github.com/manifold-systems/manifold/tree/master/man...
USD rent = 2000
EUR salary = 6000.53
USD disposableIncome = salary - rent
Then:
disposableIncome.toDecimal()
disposableIncome.toInteger()
disposableIncome.toString()
USD interest = disposableIncome.compoundInterest(startDate, endDate, apy)
USD tip = billTotal.tip(percentage)
USD and EUR would inherit from Money
Not sure how to implement it, might be a JVM magic thing.
Money cost = 25M USD;
cost += 12M EUR;
display(cost.to(EUR));
Continue this thread — 2 replies
Feels like it should be
Then methods like .plus() wouldn't even be defined for incompatible currencies. Of course that doesn't work for .equals(), so you'd still have to fall back to exceptions in that case.For instance, physical dimensions store values as SI units, regardless of supplied unit, but retain the supplied unit type as a display hint.
Here, the Length dimension illustrates working with amounts of any unit.
Length orbit = 250 mi;
Length moon = 238000 km;
Length landing = moon + orbit;
display(landing); // prints 148,136.344 mi
display(landing.to(km)); // prints 238,402.336 km
// where
display(landing.to(mi) == landing.to(km)); // prints true
// perhaps a better example
Rate rate = 1.41 km/s;
Time duration = 76 hours;
Length distance = rate * duration;
// make your own unit constants
RateUnit kps = km/s;
rate = 1.41 kps;
You can play with the manifold-science[1] project, it implements the complete SI physical dimensions and many derived dimensions along with comprehensive operator overloading[2] support etc. to make working with and reading code using quantities more productive in Java.
1. https://github.com/manifold-systems/manifold/tree/master/man...
2. https://github.com/manifold-systems/manifold/tree/master/man...
Using Java library will limit your program only to jvm platform. Using a kotlin library like this one (given it's written in pure kotlin and doesn't uses any jvm platforms specifics) will allow you to build it to whatever target kmp supports (macosx, ios, android, js, mingw...)
Plenty of languages (also Java with Graal, TeaVM, J2CL, etc.) can also target these platforms.
But as with all these languages targeting these platforms (including Kotlin) you are going to have to call platform specific API if you want to do anything useful. There are also platforms specific limitations which means certain stuff which you can do in the language doesn't work on those platforms.
All comments shown.
Couldn't load more comments. Check your connection and try again.