Announcing 'Decimal', a fixed-point decimal type and arithmetic library for Motoko

Hi Motoko devs!

I have open sourced and published my fixed-point decimal library to MOPS: https://mops.one/decimal

It provides a Decimal Motoko type, conversion to/from Text, Float, Int and Nat with rounding, arithmetic functions add, subtract, multiply, divide, power and several other utility functions.

I developed this as part of our cross-chain DeFi project daemoney (I’ll be sharing more on this very soon!). The use case was the requirement to predictably and reproducibly parse, represent, display, and cross-multiply and -divide values of currencies with differing numbers of decimals (e.g. e8s, sat, wei, USD). I couldn’t find any existing fixed-point decimal type available to use in Motoko, so developed this and now very pleased to share.

I hope you find it useful - let us know if you do, and how you’re using it.

9 Likes

As a fellow Motoko library publisher, thank you for contributing to the motoko ecosystem

1 Like

Great to see the library, thank you!

1 Like

That ChatGPT analysis seems useless/incorrect. Should be filtered by manual inspection first.

3 Likes

Skimmed the source and it looks really nice and clean. Thank you for sharing!

1 Like

Heh yes some of that GPT analysis was flawed, especially the argument about rounding! It did point out that fromText(“-”) parses to 0 - I’m not happy with that as it could mask errors in the input data so have fixed it to parse to #InvalidInput.

I made couple of other tweaks and added JSON output - version 3.0.1 is now published to MOPS.

I see. When I tried it (from your repo, not from mops) that must have had been fixed already. Because I saw it parse to an error.

Great job, thank you!