# Turn a number $x$ into a fraction with a denominator with no more than $k$ digits

Is there a function for turning any number $x$ into a fraction with a denominator that has a maximum of $k$ digits? (I'm sure there is, since Excel has one built in, I just can't figure out what it is.)

For example, $f(0.1234) =$

• $\frac{106}{859}$ where $k=3$;
• $\frac{10}{81}$ where $k=2$;
• $\frac{1}{8}$ where $k=1$.
-
A reasonably good answer can be found from the continued fraction expansion of $x$. – André Nicolas Aug 9 '13 at 22:39
I would call the continued fraction answer the best answer, in certain quantifiable ways. @AndréNicolas – Thomas Andrews Aug 9 '13 at 22:43
One could complicate things by looking at secondary convergents, but there is probably no point. – André Nicolas Aug 9 '13 at 22:48
For example, the continued fraction expansion for $0.1234$ yields convergents $0,1/8,9/73, 10/81, 19/154, 29/235, 106/859, 135/1094, 241/1953, 376/3047, 617/5000=0.1234$. – Thomas Andrews Aug 9 '13 at 22:53
@AndréNicolas, would you like to make that comment into an answer? – Joe Apr 13 '15 at 19:34