3 ms·
Are integers still a preferred method for working with and storing monetary amounts or can we go back to decimals now that most languages handle decimals decent
by pbreit 2y ago
Are integers still a preferred method for working with and storing monetary amounts or can we go back to decimals now that most languages handle decimals decently? Monetary amounts pretty much always need to eventually be presented to humans so handling them as integers is quite a pain.
- hansonkd 2y agoA decimal type is sometimes orders of magnitude slower than integer do simple stuff like summing (at least in Postgres and Python). This comes into play if you have to do aggregate functions across tens of thousands of rows and can kill performance compared to integers.
- jacobsenscott 2y agoIt isn't that common to need fractions though. Money is typically handled in integer amounts (we just divide the amounts by 100 or whatever to make it easier for humans to grok). Nobody's going to pay half a cent for a latte for example. If for some reason you do need to deal with 1/1000 of a dollar, or 1/10000 of a dollar, you can still use integers. You just need to know the scaling factor. Your presentation layer is probably always going to need to do some manipulation of the number anyway (put a dollar sign on it, etc) so using a decimal amount just for presentation needs isn't a huge win. But it will work too.