Motoko division while maintaining precision

What’s the best way to divide two Integers and maintain precision in Motoko, for example to divide two numbers with digits in the trillions.

In my specific case I want to get the percent adjustment from one number to the second number actual_value / expected_value, with 1e12 precision.

Would it be to:

  1. Scale the numerator by 1e12 numerator * 1e12
  2. Do the Integer division scaled numerator / divisor
  3. Convert the Integer to a Float
  4. Divide the Float result by 1e12.

Ideally this needs to be done on the backend.

1 Like

I always multiply by a Big Ass™️ 10^n number and then divide it back out at the end. I brought this over from my solidity days.