News for hackers. Stuff that matters.
Re:Kotlin Money by psd1 on Tuesday October 08, 2024 @10:09AM (#41777558)
> no mainstream language has a first-class data type for representing money

I don't think that's correct, absent some no-true-scotsman gymnastics.

F# has units-of-measure (UoM) out of the box, and it supports decimal numbers. I've come across a python library for UoM as well.

The big problem with handling money in code is not, IMO, the rounding (your allocate function is a nice utility but it's not core); it's unit confusion - adding baht to ren mi bi, adding cents to euros, etc. This problem is very well solved by F#'s UoM.

Re:Kotlin Money by chipdart on Tuesday October 08, 2024 @12:02PM (#41778859)
> I don't think that's correct, absent some no-true-scotsman gymnastics.

Yeah, I think OP's claim is not valid. Even .NET provides System.Decimal, which by design and explicitly mentions financial calculations.

Taken from the intro docs:

https://learn.microsoft.com/en-us/dotnet/fundamentals/runtim...

> The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors.

Re:Kotlin Money by nostrademons on Tuesday October 08, 2024 @12:14PM (#41778965)
This is often not what you want with monetary calculations. You want the rounding: you'll be making a payment of $25.86, not $25.85714285714... But you just want to make sure that the next payment is $25.85, not $25.86 or $25.857142851, and that all 7 payments together equal $181 and not $181.02.
Re:Kotlin Money by explorigin on Tuesday October 08, 2024 @12:41PM (#41779223)
I can't speak for C# but the Python Decimal type handle significant figures correctly.

> The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the “schoolbook” approach uses all the figures in the multiplicands. For instance, 1.3 * 1.2 gives 1.56 while 1.30 * 1.20 gives 1.5600.

Re:Kotlin Money by eriksencosta on Tuesday October 08, 2024 @02:34PM (#41780359)
Languages indeed have support for currencies and decimal numbers. My implementation is based on Fowler's Money pattern. The Money object is thus a value object representing a monetary amount in a given currency.

https://martinfowler.com/eaaCatalog/money.html

Re:Kotlin Money by __MatrixMan__ on Tuesday October 08, 2024 @10:27AM (#41777760)
I don't know either well, but I took a glance at both and it doesn't seem like UoM is well set up for making decisions based on which peer is going to give you a better exchange rate.

We sometimes pretend that money is measuring something, but in reality it's much messier than that.

Re:Kotlin Money by chipdart on Tuesday October 08, 2024 @12:05PM (#41778887)
> (...) making decisions based on which peer is going to give you a better exchange rate.

Neither does a money data type.

Re:Kotlin Money by __MatrixMan__ on Tuesday October 08, 2024 @03:15PM (#41780745)
It looks like multiple exchange rates per currency pair is supported: https://github.com/eriksencosta/money/blob/4bef95a1d2158e308...
Re:Kotlin Money by oblio on Tuesday October 08, 2024 @10:16AM (#41777624)
Ok, what do I do in F# if I want to not think about the low level details of FX conversions, rounding, etc? Which libraries can I use?
Re:Kotlin Money by slekker on Tuesday October 08, 2024 @11:55AM (#41778775)
Sounds interesting! How would it look like? Do you have some code to share?
Re:Kotlin Money by williamcotton on Tuesday October 08, 2024 @12:25PM (#41779070)