cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
635 stars 328 forks source link

Feature Request: add divide to Decimal type in cosmjs #1498

Open puneet2019 opened 9 months ago

puneet2019 commented 9 months ago

There is plus, minus, multiply, but there is no divide. https://github.com/cosmos/cosmjs/blob/main/packages/math/src/decimal.ts#L185-L209 Can we have divide fucntion? even if it is approximate? (approxDiv).

webmaster128 commented 9 months ago

The tricky part here is how to do rounding and the type of divisor. Decimal/Decimal, Decimal/int? Not straight forward.

But if rounding is fine for you, just call toFloatApproximation() and then operate on the JavaScript number.

puneet2019 commented 8 months ago

I think decimal/decimal, because int conversion to decimal is easy. then the final result can be converted to approx float anyway.. The problem with using floats is the 18 decimal tokens. I guess I am using decodeCosmosSdkDecFromProto for sdk.Int as well..i guess it will auto resolve when BigInts will be used..