Working with monetary data without precision loss in ArangoDB
Problem
Applications that handle monetary data often require to capture fractional unitsof currency and need to emulate decimal rounding without precision loss.Compared to relational databases, JSON does not support arbitrary precisionout-of-the-box but there are suitable workarounds.
Solution
In ArangoDB there are two ways to handle monetary data:
Monetary data as integer:If you store data as integer, decimals can be avoided by using a generalscale factor, eg.
100
making19.99
to1999
. This solution will workfor digits of up to (excluding) 253 without precision loss. Calculationscan then be done on the server side.Monetary data as string:If you only want to store and retrieve monetary data you can do so withoutany precision loss by storing this data as string. However, when usingstrings for monetary data values it will not be possible to do calculationson them on the server. Calculations have to happen in application logicthat is capable of doing arithmetic on string-encoded integers.