Algorithm for simplifying decimal to fractions
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting decimals to simplified fractions is a common task in calculators, symbolic tools, finance utilities, and educational software. The process is straightforward for terminating decimals and slightly more involved for repeating decimals. A robust algorithm should identify decimal type, construct the fraction, and reduce it with greatest common divisor logic.
Core Rule for Terminating Decimals
If a decimal has a finite number of digits after the point, you can convert it directly:
- count decimal digits
- remove decimal point to get numerator candidate
- use
10^nas denominator wherenis digit count - divide numerator and denominator by
gcd
Example for 0.75:
- numerator candidate is
75 - denominator is
100 - '
gcd(75, 100) = 25' - result is
3/4
This method is deterministic and fast.
Python Implementation for Terminating Decimals
Use string parsing to avoid floating-point rounding surprises.
This avoids binary float artifacts and gives exact rational output for decimal strings.
Repeating Decimal Conversion
For repeating decimals, use algebraic subtraction.
Suppose value is x = 0.1(6) where repeating block is 6.
- non-repeating length is
1 - repeating length is
1
Formula approach:
- '
10^(a+b) * x - 10^a * x = repeating_integer - prefix_integer'
Where:
- '
ais non-repeating digit count' - '
bis repeating block length'
Then simplify resulting fraction by gcd.
Python Function for Repeating Decimals
Represent repeating decimals with two parts: prefix and repetend.
This yields exact rational forms for recurring patterns.
Use Continued Fractions for Approximation
When input is an imprecise floating number, exact conversion may not be possible or desired. In that case, use continued fractions to find the best approximation under a denominator limit.
This is practical for measurement data and noisy numeric input.
End-to-End Strategy in Real Software
A production converter usually follows this sequence:
- detect whether input is exact decimal string, repeating form, or float
- pick exact conversion path when possible
- apply gcd simplification
- normalize sign to numerator
- optionally return mixed number format for display
Keep internal representation as numerator and denominator integers for predictable math behavior.
Validation Tests You Should Keep
Key tests:
- '
0.5 -> 1/2' - '
2.0 -> 2/1' - '
-1.25 -> -5/4' - repeating
0.(3) -> 1/3 - repeating
12.34(56)expected exact fraction
These tests protect against parsing regressions and sign mistakes.
Common Pitfalls
- Parsing through binary float first and losing exact decimal precision.
- Forgetting to simplify fraction by greatest common divisor.
- Mishandling sign placement between numerator and denominator.
- Mixing exact conversion and approximation logic without clear API behavior.
- Ignoring repeating-decimal input formats in user-facing tools.
Summary
- Terminating decimals convert directly using base-ten denominator logic.
- Repeating decimals require algebraic subtraction method for exact fractions.
- String-based parsing is safer than float-first conversion for exactness.
- Continued-fraction style approximation is useful for noisy float inputs.
- Always normalize and simplify output for stable downstream computation.

