Working with monetary data without precision loss in ArangoDB
Problem
Applications that handle monetary data often require to capture fractional units of currency and need to emulate decimal rounding without precision loss. Compared to relational databases, JSON does not support arbitrary precision out-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 general scale factor, eg.100
making19.99
to1999
. This solution will work for digits of up to (excluding) 253 without precision loss. Calculations can 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 without any precision loss by storing this data as string. However, when using strings for monetary data values it will not be possible to do calculations on them on the server. Calculations have to happen in application logic that is capable of doing arithmetic on string-encoded integers.