Three way currency integer conversion with power of ten

45 Views Asked by At

I need to convert crypto currencies which have different decimals only by using their integer representation.

Imagine the following use case in their smallest integer representation:

  • XMR: $1e12$
  • BTC: $1e8$ (satoshis)
  • USD: $1e2$ (cents)

with the following conversion rates:

  • XMR/BTC: $0.02845543$
  • BTC/USD: $8435.38$

And I need to how the XMR/USD exchange rate by using integers only.

The only solution I came up requires a division and as such will result in using a float number.

Is there any formula to do this conversion using powers of ten ?

1

There are 1 best solutions below

0
On

You have $\text{ XMR }1 = \text{ BTC } 0.02845543 = \text{ BTC } \dfrac{2845543}{100000000}$

and $\text{ BTC }1 = \text{ USD } 8435.38 = \text{ USD } \dfrac{843538}{100}$

so $\text{ XMR }1 = \text{ USD } \dfrac{2845543\times 843538}{100000000\times 100}= \text{ USD } \dfrac{2400323651134}{10000000000}$

and the next stage is to approximate to cents by saying $\text{ XMR }1 = \text{ USD } \dfrac{24003}{100} + \dfrac{23651134}{10000000000}$ where you can get the $24003$ number by using integer division, dividing $2400323651134$ by $100000000$

Whether you round up to $\text{ XMR }1 \approx \text{ USD } 240.04$ or round down to $\text{ XMR }1 \approx \text{ USD } 240.03$ is up to you. Integer division will typically round down