Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The question is why? What does the double give you over a 64-bit integer? Sure when you divide and it leaves a fractional part you loose it and need to think about what happens there explicitly but you need to do the same for doubles to avoid pennies going missing and snowballing into larger errors.


Using integer representations for currencies becomes very messy when dealing with more than one currency at a time.

The United States dollar is famously subdivided into cents, but is also subdivided into 'mills' (one thousand to the dollar).[0]

The Mauritanian ouguiya is divided into five khoums.

The Madagascan ariary is divided into five iraimbilanja.

The Maltese scudo is divided into twelve tarì, which are divided into twenty grani, which are divided into six piccioli.

Historically, such currencies were ubiquitous. For example, prior to 15 February 1971, the pound sterling was divided into twenty shillings, each of which was divided into twelve pence, a system that originated with Roman currency and was used throughout the British Empire.

Exchange rates are typically quoted in terms of the largest unit, whereas integer representations of currency would need to be done in terms of the smallest unit, so extensive information about currency structures would need to be used to correctly represent exchange rates. Floating point or binary-coded decimal representations are consequently much better.

[0]: https://en.wikipedia.org/wiki/Mill_(currency)


There are multiple factors:

Doubles can exactly represent all 16 digit ints (IIRC) which is good enough for most use cases, you can catch the out of range cases (as you should do with ints as well)

If you use long ints you must track the decimal precision along the value which is not always trivial if you use mixed currencies.

Long ints are not guaranteed to correctly roundtrip through json serialisation / deserialisation

Doubles are easier to handle in the frontend

Currency math is different enough from regular math that you need special operator functions anyway so it’s not like ints are easier to handle either


Using ints forces you to deal with all that. Using floats lets you easily ignore it and sweep it under the rug.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: