A decimal can be written as a fraction if and only if it represents a
rational number. That's pretty much the definition of "rational number". One can also show that only terminating or "eventually repeating" decimals represent rational numbers.
For example, a terminating decimal can be written as
x=a1a2a3⋅⋅⋅am.b1b2b3...bn where the "a"s represent the digits before the decimal point and the "b" represent the n digits after the decimal point. Then
10nx=a1a2a3⋅⋅⋅amb1b2b3⋅⋅⋅bm an integer. Dividing both sides by
10n gives the fraction
10na1a2a3⋅⋅⋅amb1b2b3⋅⋅⋅bn. There may be some power of 2 or 5 that is a factor of the numerator and so will cancel with a 2 or 5 in the denominator but even so, we have it written as a fraction with only 2s and 5s in the denominator. For example, if x= 3.816, then 1000x= 3816 so that
x=10003816=8(125)8(477)=125477
Repeating decimals are a little harder. Suppose
x=a1a2a3⋅⋅⋅am.b1b2b3⋅⋅⋅bnc1c2c3⋅⋅⋅cpc1c2c3⋅⋅⋅cp⋅⋅⋅ where that last "
⋅⋅⋅" indicates that the "c" digits just keep repeating.
First, move the "b" digits out of the decimal part by multiplying by
10m:
10nx=a1a2a3⋅⋅⋅amb1b2b3⋅⋅⋅bn.c1c2c3⋅⋅˙cpc1c2c3⋅⋅⋅
Now multiply by another
10p to move that first group of "c"s out:
10m+p=a1a2a3⋅⋅⋅anb1b2b3⋅⋅⋅bmc1c3c3⋅⋅⋅cpc1c2c3⋅⋅⋅c2c2c3⋅⋅⋅cp⋅⋅⋅
It is important to understand that because the "c" portion keeps repeating the "c" on the right never end so the decimal part of those two numbers is the same. Subtracting, we have
(10m+p−10m)x=a1a2⋅⋅⋅anb1b2⋅⋅⋅bmc1c2⋅⋅⋅cp−a1a2⋅⋅⋅anb1b2⋅⋅⋅bm. That's rather complicated but we have an integer times x equal to an integer and so x can be written as a fraction.
An easy example is
0.3 where the overline indicates the 3 repeats- that is x= 0.3333.... Then 10x= 3.3333... and, subtracting 9x= 3 so that x= 3/9= 1/3.
A little more complicated example would be
x=323.7591154=323.759115411541154.... The non-repeating part decimal part, 759, has three decimal places:
103x=1000x=323759.115411541154.... The repeating part, 1154, has four decimal places:
103+4x=10000000x=3237591154.11541154.... Subtracting,
9999000x=3237267395 and
x=99990003237267395