Settings

Theme

Decimal Is Sometimes Wrong

jt-hill.com

10 points by jt-hill 2 months ago · 3 comments

Reader

benrutter 2 months ago

I enjoyed reading this! It's not always possible but finance data that translates directly into cents/euro-cents/pence/yen/etc is really nice to work with as integers. Obviously as soon as you need to divide that stops being such a solid strategy though. . .

  • jt-hillOP 2 months ago

    Thanks! And you're right this is true for storage and at the transaction level where nothing smaller than, e.g., a penny is allowed to move or exist in the first place. But when you're working with interest rates and the main calculations involve multiplying by numbers < 1, you still have to figure out rounding.

jt-hillOP 2 months ago

Author here. I thought was doing what I was supposed to do by using Decimals heavily in my credkit[0] Python library, but I did some testing and discovered it wasn't worth the pain for me. Test code [1] is available for the curious.

The moral of the story is that "best practices" are not a substitute for using your brain.

[0]: https://github.com/jt-hill/credkit

[1]: https://gist.github.com/jt-hill/839e1a6c51d28cbcb8fc69e61431...

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection