Last updated
How Decimal-to-Fraction Conversion Works
Converting a terminating decimal to a fraction is straightforward: write the decimal digits as the numerator and the appropriate power of 10 as the denominator, then simplify using the Greatest Common Divisor (GCD).
0.75 → 75/100 → GCD(75,100) = 25 → 3/4
0.625 → 625/1000 → GCD(625,1000) = 125 → 5/8
0.333... → repeating decimal → 1/3 (exact)
0.142857142857... → repeating → 1/7 (exact)
Repeating Decimals
Repeating (recurring) decimals represent rational numbers that can't be expressed as
terminating decimals. To convert a repeating decimal like 0.̄3 (0.333...):
- Let x = 0.333...
- Multiply both sides by 10: 10x = 3.333...
- Subtract: 10x − x = 3.333... − 0.333... → 9x = 3
- Solve: x = 3/9 = 1/3
For 0.̄142857 (repeating block of 6 digits), multiply by 106 = 1,000,000
and subtract to get 999,999x = 142,857, so x = 142857/999999 = 1/7.
Common Decimal-to-Fraction Reference
| Decimal | Fraction | Percentage |
|---|---|---|
| 0.1 | 1/10 | 10% |
| 0.125 | 1/8 | 12.5% |
| 0.25 | 1/4 | 25% |
| 0.333... | 1/3 | 33.3̄% |
| 0.375 | 3/8 | 37.5% |
| 0.5 | 1/2 | 50% |
| 0.625 | 5/8 | 62.5% |
| 0.666... | 2/3 | 66.6̄% |
| 0.75 | 3/4 | 75% |
| 0.875 | 7/8 | 87.5% |
The GCD Algorithm
Simplifying a fraction requires finding the Greatest Common Divisor of numerator and denominator. The Euclidean algorithm is the most efficient method:
// Euclidean algorithm
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
function decimalToFraction(decimal) {
const decStr = decimal.toString();
const decPlaces = (decStr.split('.')[1] || '').length;
const denominator = Math.pow(10, decPlaces);
const numerator = Math.round(decimal * denominator);
const divisor = gcd(numerator, denominator);
return { n: numerator / divisor, d: denominator / divisor };
}
console.log(decimalToFraction(0.75)); // { n: 3, d: 4 }
console.log(decimalToFraction(0.625)); // { n: 5, d: 8 }