kryptovero.github.io icon indicating copy to clipboard operation
kryptovero.github.io copied to clipboard

Use Decimal instead of floating point to calculate monetary values

Open jehna opened this issue 4 years ago • 4 comments

Now we're using the Javascript native floating point numbers for calculations, and they should not be used for calculating monetary values because they cannot represent all necessary values (losing precision at some places, like the infamous .2 + .1)

jehna avatar Jul 21 '21 16:07 jehna

JS has native support for BigInt, and it would suffice if we could limit the number of decimals to e.g. two (to calculate cents instead of euros).

However some (most?) coins act with much more precision, so we'd be better with something that supports arbitrary precision with correct decimal handling.

jehna avatar Jul 21 '21 16:07 jehna

I'm also thinking a bit about rewriting the ledger/ package with Rust (or similar) and building it into WASM that could be used 1-to-1 replacement for the current core. This is because Rust and other proper programming languages have support for operator overloading, which makes Decimal support more pleasant to use.

Edit: So in JS where we don't have oerator overloading we'll need to do something like x.dividedBy(y).plus(z).times(9) (decimal.js), while proper languages like Rust can create a decimal object that overloads arithmetic operators so the syntax is the "normal" x / y + z * 9

jehna avatar Jul 23 '21 18:07 jehna

Currently thinking between:

  • Rust (good support for wasm, good type system, but borrow checker can be a pain to work with)
  • Typescript with decimal library (no full rewrite, but no operator overloadin support, serialisation issues with JSON?)
  • Dart (easy language, native JS interop, possibility to rewrite whole app with Flutter, language wide push for getting rid of nulls)
  • Scala (good type system, seems to be behaving reasonably well with JS, but may restrict contributions for being exotic and I'm not sure if I want to work with SBT and nulls from possible Java interop)

jehna avatar Sep 11 '21 14:09 jehna

So I started working on porting the ledger package to Dart. Using the Decimal package for Dart to handle monetary values.

Will push and link the working branch here later.

jehna avatar Oct 14 '21 20:10 jehna