Algorithm: turining a fraction into a decimal expansion string
All we need is a function $f$ that gives the whole part of a fraction. One can define this one using integer division:

$$f(a, b) = \lfloor a / b\rfloor$$

Finally let's assume we have routine $g(a, b)$ that reduces a fraction to its simplest form. This can be done with the Euclidean algorithm.

Let's first take the integer part of a fraction out using $f$, and reduce our fraction using $g$, giving us $\frac{a}{b} &lt; 1$. Then we can repeatedly ask for the next decimal $d$ and continue with $10\cdot \frac{a}{b} - d$:


$d := f(10a, b)$
$a, b := g(10a - b\cdot d, b)$


This algorithm will perfectly give you all digits ad infinitum, and $g$ makes sure the numbers stay small and manageable.