Ask HN: Store price as integer or decimal? For an e-commerce website, how would you store products' price in DB? decimal? pence integer? |
Ask HN: Store price as integer or decimal? For an e-commerce website, how would you store products' price in DB? decimal? pence integer? |
While you're at it, keep a currency-symbol/code together with your cents in the same value-object. Just like time is incomplete without a time zone, money is incomplete without a currency. You will thank me later.
For inspiration, [RubyMoney](https://github.com/RubyMoney/money) is a very well-designed library dealing with this in the context of Ruby.
+ https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.ht...
>The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data. In MySQL, NUMERIC is implemented as DECIMAL, so the following remarks about DECIMAL apply equally to NUMERIC.