logoalt Hacker News

m00xyesterday at 7:57 PM2 repliesview on HN

You can't do everything you need with an integer. There are values you might want to display or calculate with that are smaller than cents. In some places you'll need things like BigDecimal, which are immune to floating point errors in most cases.

It's also safe to return decimal values for displaying values.


Replies

solomonbyesterday at 9:59 PM

The integer `1` can mean whatever you want, it doesn't need to be a cent. Haskell's `Fixed` type is a good example of this:

https://hackage-content.haskell.org/package/base-4.22.0.0/do...

Its a wrapper around an `Integer` where you declare the scale in the type. So if you use `Fixed E2` as your type then `MkFixed 1` is 1 cent. If you did `Fixed E3` as your type then `MkFixed 1` is 0.1 cent. In both cases it is entirely an integer encoding.

FabHKtoday at 3:17 PM

But it's a good idea to keep those values distinct from actual money. You can't pay a fraction of a cent to anyone.

show 1 reply