News for hackers. Stuff that matters.
Re:Kotlin Money by willcipriano on Tuesday October 08, 2024 @03:55PM (#41781183)
I'd like types for all the currencies.

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.

Re:Kotlin Money by owlstuffing on Tuesday October 08, 2024 @05:32PM (#41782033)
What you want is Money, currency is a _unit_ type of a Money quantity, just as meter is a unit type of Length.

Money cost = 25M USD;

cost += 12M EUR;

display(cost.to(EUR));

Re:Kotlin Money by wtetzner on Wednesday October 09, 2024 @12:15AM (#41784451)
I don't think this makes sense? You need to specify an exchange rate somewhere.

> just as meter is a unit type of Length

The difference is that length units are fixed, but exchange rates are constantly changing.

Re:Kotlin Money by owlstuffing on Wednesday October 09, 2024 @01:55AM (#41784918)
> You need to specify an exchange rate somewhere.

Right. As I mentioned earlier exchange rate services would need to be integrated into the API.

Also, exchange rate services would not be needed until a conversion is necessary. For instance, when adding money of different currencies internally the Money type maintains currencies separately, subtotals for USD, EUR, etc. are managed. If at a later time the sum of Money should be displayed, deposited, etc. as a single currency, only then is an exchange service involved.