News for hackers. Stuff that matters.
Re:Kotlin Money by hathawsh on Tuesday October 08, 2024 @01:42PM (#41779858)
I have questions about some edge cases I've encountered in dealing with money.

- Let's say I load two money values from a database. Value A is 1 USD and value B is 1 BTC. If I try to add them together, what happens? (I would expect a runtime exception. In fact, I would hope for a runtime exception, because the operation usually indicates a programming error.)

- If I divide $2.00 by 3, do I get $0.66 or $0.67? If I want to specify the rounding rule, is there a simple way to do it? I see there's support for allocation by percentage, but sometimes the allocation rules are more complex and I need to be able to specify the rounding rule.

- Does this library help parse user input? When parsing user input, what happens to extra digits? Does "0.015" get interpreted as $0.01 or $0.02?

Edit: one more question. Sometimes people bend the rules on the number of digits; for example, gas stations may charge $3.599 per gallon. Can the library deal with that or would I have to convert to decimal, multiply, and then convert back to money?

Re:Kotlin Money by eriksencosta on Tuesday October 08, 2024 @02:23PM (#41780249)
Thanks for the questions. Answers:

1. You get an exception

2. You may specify the rounding rule: https://github.com/eriksencosta/money/blob/trunk/docs/usage/...

3. It has some parsing capabilities. The extra digit is kept, rounding is only applied when doing an operation with the Money object

4. Same as 2