How to Convert a Decimal to a Fraction

Finds the fraction closest to a decimal, keeping the denominator within the limit you set. A decimal such as 0.375 comes out exactly as 3/8, while a number such as pi gives the nearest approximation. The difference from the original decimal is also shown.

The fraction closest to a decimal is found, keeping the denominator within a limit you set. A decimal that terminates comes out exactly, with a difference of 0. A number such as pi, which no fraction represents exactly, gives the nearest one the limit allows.

pqx(qqmax)\dfrac{p}{q} \approx x \quad (q \le q_{\max})

xx is the decimal, qmaxq_{\max} the largest denominator allowed and pq\dfrac{p}{q} the answer. It comes back in lowest terms, with the mixed number and the difference from the original decimal.

Example

Converting 0.375 with a limit of 1000, 0.375=37510000.375 = \dfrac{375}{1000} reduces to 38\dfrac{3}{8}, and the difference is 0. Since it is exact, lowering the limit to 10 gives the same answer.

Converting pi with a limit of 1000 gives 355113\dfrac{355}{113}. The difference is about 0.0000003, so the two agree to six decimal places.

Choosing the limit

A larger limit finds a closer fraction, but the denominator grows with it and the answer gets harder to use. A fraction such as 355113\dfrac{355}{113}, which fits well with a small denominator, holds its place over a wide range of limits.

A repeating decimal written out part way, such as 0.3333333333, still returns 13\dfrac{1}{3} at a limit of 1000.

Notes

The limit must be a whole number of at least 1, and no more than 100000.

The sign of the difference says which side the fraction falls on. A negative difference means the fraction is the larger of the two.