atomicdata-dev / atomic-data-docs

Atomic Data is a specification to make it easier to exchange data.
https://docs.atomicdata.dev
MIT License
17 stars 7 forks source link

Currency datatype / property #148

Open joepio opened 1 year ago

joepio commented 1 year ago

Representing financial data has some interesting problems. In javascript, 0.3+0.6 = 0.899999999999. If you're building a webshop, and forget that this problem exists, you might make a painful mistake when rendering shopping cart totals. This problem is caused by how JS numbers are represented in memory (as floats).

Atomic Data distinguishes Integers from Floats. So you might say: let's store monetary data as integers! We could use the amoutn of cents, and that would work in most cases. However, what about very small prices that deal with fractions of a cent? Now we have no way of representing that.

We could also use a String datatype. That way, whatever the user inputs, is what is being stored. If they want a certain level of precision, they can just add characters. But know that the second we have to make calculations, we can still make these errors.

I'm not quite sure what is the best approach here. What should we recommend users do when they are modelling Classes with a currency-dependent property?