I'm aware of a few good options:
Big integers (e.g., int64_t, mpz_t, any bignum lib) to represent cents or 10-n cents—say, an integer represents 1/100 of a penny ($1.05 == 10500). This is called a scaled integer.
High level library for arbitrary precision decimal arithmetic such as BigDecimal in Java, Decimal in Python, decimal.js in Javascript, boost::multiprecision in C++
Strings.
Packed BCDs (binary coded decimals) is a more esoteric method that seemed popular in old software. Read more about it.
In production code for banks (or credit cards, ATMs, POS systems), what data type is actually used the most? I'm especially asking those who worked for banks.
EDIT: Super useful links for those with the same problem domain (needing to implement a "money" data structure that doesn't break).
- http://martinfowler.com/eaaDev/quantity.html
- http://www.codeproject.com/Articles/28244/A-Money-type-for-the-CLR
- http://c2.com/cgi/wiki?MoneyObject
- http://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency
- http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
EDIT for the fellow who said this is a duplicate question: This is a practical not a theoretical question of "what's the best". Read the unedited title of my question. I'm asking what people have seen first-hand in banks' codebases.
I know BigDecimal is "best" obviously, but nice APIs like that aren't available everywhere, believe it or not, and decimal libraries are expensive as opposed to ints.