## Introduction: Decimal to Fraction

You're here because you have a question, a math question.

I don't know who asked you this math question. Maybe it was your math teacher, or your mother, or a friend, or your employer. Or maybe this is a math question you have asked yourself.

Still the question remains, like a splinter in your mind:

Decimal to Fraction?

The answers are out there Neo, and they're waiting for you. When you're ready, this instructable will help those answers to find you.

By the way, this phrase, "Decimal to Fraction", denotes a topic, one of several in an official Instructables contest (the details of which I found here: https://www.instructables.com/contest/burningquestions65/ ) Also per the rules of this contest, the phrase "Decimal to Fraction" is the title of this Instructable.

Anyway, I should get back to explaining, or perhaps guessing, why you're here.

You are here because you wish to covert a positive rational number in decimal form into a fraction, a ratio of two integers. Or perhaps you want the answer in the form of a mixed fraction, the sum of an integer and a proper fraction.

Below I have written some question-answers, with the decimal representation "question" part on the left side of these equations, and the "answers" in fraction and mixed fraction forms, on the right side.
`3.125 = 3125/1000 = 25/8 = 3 + 125/1000 = 3 + 1/8 0.00314 = 314/100000 = 157/50000 3.141414... = 311/99 = 3 + 14/99 3.456666... = 3111/900 = 1037/300 = 3 + 137/300`

Is this the form of the question you had in mind?

Step 3 and Step 9 demonstrate methods for the easy case of converting a regular, non-repeating, decimal number.

Methods for converting a repeating decimal number are revealed in Step 7, and using a slightly different trick in Step 11. The method shown in Step 7, the Subtraction Trick, is best if you want your answer left in the form of an improper fraction. The method in Step 11 quickly takes you to an answer in mixed fraction form.

The other Steps, {1,2,4,5,6,8,10,12}, are there to explain everything else that I felt was worth explaining.

There's a very good chance you are reading this by way of a computer of some kind. If you already own a computer, and you have a burning desire to solve math problems, there's no reason why you should not have a copy of Octave. Octave is an advanced numerical problem solving tool. It's the open-source clone of a commercial product called MATLAB. Basically Octave is a mutant calculator on steroids! If you want to do some serious number crunching, and you want to do it for free (minus the effort of installing it and learning how to use it), you want a copy of Octave. It's waiting for you, here:
http://octave.sourceforge.net/

This instructable might quote some Octave code in a few places. The same commands usually work with MATLAB. Also your expensive graphing calculator might be able to do some, but not all, of the tricks Octave/MATLAB can do.

Of course for the purist, there's always pencil and paper...

## Step 1: In the Beginning...

Building bigger numbers out of smaller numbers is one of the grand traditions of mathematics.

In the beginning there were only ten numerals, the set {0,1,2,3,4,5,6,7,8,9}.

Then came the larger decimal integers, constructed from non-negative powers of 10, for example:

1024 = 1*103 + 0*102 + 2*101 + 4*100

Then came the fractions, also called ratios. A fraction is a number constructed from two numbers connected by a division operator. Like this:

3/4

The number on top is called the numerator, and the number on the bottom is called the denominator.

In the beginning, fractions only had integers for their numerators and denominators, and even today the oldest and noblest of fractions are still ratios of two integers.

The invention of fractions made it possible to express positive numbers smaller than 1. Even today those fractions absolutely smaller than 1 are honored with the title proper.

Then came negative numbers.

Then came exponents.

Then came the decimal point.

The decimal point gave decimal numbers the ability to become smaller than 1, like fractions could. e.g.

0.25 < 1

The breakthrough that made this possible were fractional powers of 10, {1/10, 1/100, 1/1000, ...}, or equivalently the powers of 10 with negative exponents.

0.25 = 2*10-1 + 5*10-2

Remarkably, a decimal number, with digits on both sides of the decimal point, can express its bigness and its smallness at the same time!

1024.25

1024.25 = 1*103 + 0*102 + 2*101 + 4*100 + 2*10-1 + 5*10-2

## Step 2: Some Notable Properties of Decimal Numbers

Multiplying a decimal number by 10n shifts the decimal point n places to the right.

E.g.

1024.25 * 101 = 10242.5

1024.25 * 102 = 102425

A decimal number can be thought of as a weighted sum with a term for each decimal place.

1024.25 = 1*103 + 0*102 + 2*101 + 4*100 + 2*10-1 + 5*10-2

Or it can be thought of a the sum of only two terms: an integer part and a fractional part.

1024.25 = 1024 + 0.25

The integer part consists of every digit to the left of the decimal point. The fractional part consists of the decimal point and every digit to the right of the decimal point.

## Step 3: Converting a Regular (non-repeating) Decimal Number to a Fraction

Converting a regular (non-repeating) decimal number to a fraction is not difficult.

Consider
`r = 3.125`
First, I make this number the numerator of a fraction whose denominator is 1. That is to say r = r/1.
`r = 3.125 = 3.125/1`
Next I want to multiply the numerator and the denominator by a power of 10 large enough to shift the fractional part of the decimal into integer territory.

To figure out how large of a power of 10 I will need, I count the number of digits to the right of the decimal point, excluding trailing zeros. For r = 3.125, there are 3 digits to the right of the decimal, so I multiply numerator and denominator by 103 =1000 to get:
`r= r/1 = 3.125/1  * 1000/1000 = 3125/1000`
Now if you want to, you can reduce this fraction, so that the numerator and denominator are coprime. Find any common prime factors and remove them.
`r = 3125/1000= (5*5*5*5*5)/(2*2*2*5*5*5) = (25*125)/(8*125) =  25/8`

By the way, for factoring that numerator and denominator, I got a little help from a close, personal friend of mine, Octave, the numerical problem solving application I mentioned in the introduction. It has a command called "factor()" for finding prime factors. It also has a command called "gcd()" that can find the "greatest common divisor". Together these commands make short work of any fraction reducing tasks. A screen shot of this is shown in the second picture for this Step.

Another example:
`r = 0.006100`
Count the number of digits to the right of the decimal point, excluding trailing zeros. The sequence {0061} has 4 digits. So multiply numerator and denominator by 104 = 10000 to get.
`r = 0.0061/1 * 10000/10000 = 61/10000`

## Step 4: Long Division: the Birthplace of Repeating Decimals

Decimal numbers with repeating decimals appeared in our universe soon after long division was invented.

Consider the long division of 20 by 9.

First 9 goes into 20, 2 times, with a remainder of 2. That 2 has a trailing zero, so the dividend for the next step is 20.
Then 9 goes into 20, 2 times, with a remainder of 2. That 2 has a trailing zero, so the dividend for the next step is 20.
Then 9 goes into 20, 2 times, with a remainder of 2. That 2 has a trailing zero, so the dividend for the next step is 20.

...and the answer keeps growing, or at least the trail of 2s to the right of the decimal point keeps growing.
`20/9 = 2.222222...`
As you add more 2s behind the decimal point the sum gets larger, but only by minuscule amounts. Actually the sum is approaching a particular number, and that number is exactly 20/9, and it turns out the exact value of 20/9 is a number that cannot be written exactly in decimal form, or at least not without an infinite number of 2s.

To borrow a phrase from the field of computer science, the problem with repeating decimals is sort of a "bug". It's a bug inherent in the design of decimal numbers. As far as I can tell this bug cannot be "fixed". However there is a workaround.

The workaround is to draw a little bar over the sequence of digits that repeats, and then call it good.

The little bar means "the sequence of digits under this bar repeats forever". Seriously. That's what it means.

## Step 5: The Little Bar Convention for Writing Repeating Decimals

In the text of this explanation, I have decided to I put the repeating part of the decimal in square brackets, like this:
`20/9 = 2. = 2.222222....`
The little bar and the square brackets mean exactly the same thing, but the square brackets are easier to type. The illustrations use the traditional bar notation, if you want to see what it looks like. Let me provide a few more examples to make this bracket-bar convention clear.
```1/3  =  0.  = 0.3333...
1/6  =  0.1 = 0.1666...
5/11 =  0. = 0.45454545...
1/7  = 0. = 0.142857142857142857...
4/7  = 0. = 0.57 = 0.571428571428571428...```
I should also mention that I have tried to make tried to write these expressions as compactly as possible. That is to say, by making the repeating sequence, and the non-repeating sequence, as short as I can make them. But if I wanted to I could write them less compactly, like so:
`1/3 = 0. = 0.3 = 0.33, etc.`
But 1/3 = 0.  is the shortest, least characters, most compact, way to write this.

Also notice: writing
`4/7 = 0.57`
takes a few more characters than writing 4/7 = 0. , but I did that to demonstrate 4/7 and 1/7 actually share the same repeating sequence of digits, namely 

## Step 6: Abusing the Little Bar

The repeating part of a repeating decimal number always appears on the end of the number, to the right of the decimal point, and to the right of every other figure. Putting the little bar anywhere else will lead to trouble, or a meaningless expression.

## Step 7: The Subtraction Trick for Converting Repeating Decimals to Fractions

Consider the repeating decimal number:
`r = 3. = 3.141414...`
I don't have to multiply this by an infinite power of 10. It turns out that I only need to multiply by a power of ten with the same magnitude as the length of the repeating part.

For this example the repeating sequence, , is n=2 digits long. So I multiply r by 10n = 102 = 100 and get
`100r = 314. = 314.141414...`
The reason I've explicitly written equations for r and 100r, is that I want to subtract one equation from the other and make the messy repeating decimal disappear, leaving me with just an integer:
`(100r - r) = 99r = 314. - 3. = 311`
Then I solve for r.
`r = 311/99`
This is already reduced, since 311 and 99 are coprime, since 311=311 and 99=3*3*11.

Here's another example:
`r = 0.`
What is the length of the repeating part? The repeating sequence is 6 digits long, so multiply r by 106, and get
`1000000r = 142857`
Then subtract these two equations and solve for r.
```(1000000r - r) = 999999r = 142857
r = 142857/999999```
Reducing this fraction is going to take some doing. Again I am using Octave's "factor()" and "gcd()" commands to help with the heavy lifting.

It turns out that:
`r = 142857/999999 = (3*3*3*11*13*37)/(3*3*3*7*11*13*37) = 1/7`

So what about those occasions when there is a sequence to the right of the decimal that does not repeat, followed by a sequence that does repeat? I think that is the the most general case, and the hardest decimal-to-fraction problem to solve. For example:
`r= 3.45`
There are couple of ways to do this one. One way is to find 1000r, 100r, and then subtract these to get 900r.
```     r=  3.45
1000r = 3456.
100r =  345.
900r = 3111
r = 3111/900 = 3*1037/3*300 = 1037/300```

## Step 8: Improper Fractions, Proper Fractions, and Mixed Fractions.

Some of you reading this may feel a little uncomfortable calling an expression like,
`1037/300`
a fraction. I mean it has a numerator and a denominator, and it has a division operator, but still, some people don't want to call it fraction. Why? Because it has a value greater than 1, and in common usage the word "fraction" means a portion of something necessarily less than the whole; i.e. something less than 1.

Suppose for example a house painter tells you that he can paint your house for "a fraction of the price" compared to a competing house painter. That usually doesn't mean he is going to charge you more than the competitor. But if the "fraction" he had in mind was 4/3, then that's exactly what it would mean. His price would be the competing price plus 1/3 of the competing price. Or suppose there's a surgeon who wants to cut out a "fraction" of my liver to transplant it into my brother. It's not possible for that "fraction" to be 4/3, or 3/2, or any number greater than 1.

Because of this notion that in many cases the word "fraction" should mean "less than 1", and probably for other reasons, mathematicians came up with the definition of a proper fraction. A fraction is said to be proper if and only if it is strictly less than 1, which is equivalent to the numerator being strictly less than the denominator.

Example: 0.  = 14/99 is a proper fraction because 0.  = 0.141414... < 1, or equivalently, 14/99 is proper because 14 < 99

An improper fraction is any fraction greater than or equal to one, or equivalently, any fraction whose numerator is greater than or equal to its denominator.

Example: 3.125 = 25/8 is an improper fraction because 3.125 > 1, or equivalently, 25 > 8

Converting improper fractions to mixed fractions

If you don't like improper fractions, because they're so darned improper, you can always rewrite an improper fraction as a mixed fraction. A mixed fraction is the sum of an integer and a proper fraction,. The process for doing this is essentially long division.

For example: 311/99 is an improper fraction, and you can rewrite it as:

311/99 = (297+14)/99 = (3*99 + 14)/99 = 3 + 14/99

The integer part is 3. The proper fraction part is 14/99.

## Step 9: Decimal Numbers to Mixed Fractions

If you happen to start with a number in decimal form, this process of finding a mixed fraction is much, much, easier.

With a number in decimal form, you can find the integer part and the proper fraction part, essentially from inspection. All the digits to the left of the decimal point represent an integer, and all the digits to the right of the decimal point represent a proper fraction.
`3.125 = 3 + 0.125`
By the way, concerning this number formed from only the digits to right side of the decimal point, we know it represents a proper fraction because it has to be less than 1.

0.125 represents a proper fraction since 0.125 < 1

Now, I know what some of you are thinking. You're thinking:

"Dammit, Jack! What about all those previous examples where I started out with a decimal number with a non-zero integer part? Couldn't I have just subtracted out the integer part first? Like 3.125? Wouldn't it simplify things considerably to subtract out the 3 first?"

Yeah, sure. That's cool ese. You could definitely do it that way.In fact, subtracting out the integer part first will be especially helpful if a mixed fraction is the form you are working towards.

For example, let us re-examine:
`r = 3.125`
This time, write it as the sum of its integer part and its fractional part.
`r = 3 + 0.125 = 3 + 125/1000 = 3 + 1/8`

## Step 10: A Formula for "pure" Repeating Decimals

You might have noticed the denominators of the fraction forms of these repeating decimal numbers usually have a whole bunch of nines in them. For example:
```0.      =      6/9
3.     =    311/99
0.     =     45/99
0. = 142857/999999
3.45    =   3111/900
0.012  =   1222/99000```
I mean, before these fractions get reduced, they have a whole bunch of nines in them. The reason why is the denominator always comes from the difference of two powers of 10 used during the subtraction trick shown in Step 7, e.g. 10-1=9, 100-1=99, 1000000-1=999999, 1000-100=900

Now, consider the special case of those repeating decimals, for which the integer part is zero, and there is no non-repeating part. That is to say the repeating part is shoved right up against the decimal point, like the examples below:
```0.   =            1/9   = 1/9
0.   =            2/9   = 2/9
0.   =            3/9   = 1/3
0.   =            9/9   = 1
0.  =           01/99   = 1/99
0.  =           09/99   = 1/11
0.  =           45/99   = 5/11
0. =          123/999   = 41/333
0.=         5544/9999  = 56/101
0. =    142857/999999 = 1/7```
Do you see the pattern? The numerator is the repeating sequence. The denominator is an integer composed entirely of nines, and it has the same length as the repeating sequence.

For this trick to work, the repeating fraction has to be in the form shown above, specifically it has to have zero integer part, and it has have the repeating part immediately following the decimal point. I don't know if there is any official math lingo for this form, but for the purposes of this instructable, I am going to call this form a "pure repeating decimal"

Why call it pure? I call it this repeating decimal, "pure", because it has no integer part, and it has no non-repeating part. The only part it has, is the repeating decimal part.

Obviously it deserves a special name since it has this property of being equal to a fraction with the repeating sequence, as an integer, for the numerator, and a denominator of the same length as the numerator, composed of all nines, i.e. it is (10n -1), where n is the length of the sequence.

The other interesting property of pure repeating decimals, is they are almost always proper, i.e. less than 1.

The only example of an improper pure repeating decimal is:
`0. = 0.99999999.... = 9/9 = 1`
0.  is improper because it is not less than 1.

This improperness can lead the way to some weird results. For example the expression:
`1. `
does not have a proper fraction part. Suppose I want to write this as a mixed fraction. I just have accept the fact that all the stuff on the right side of the decimal point is also equal to 1.
`1.  = 1 + 0.  = 1 + 1 = 2`

The second picture for this Step shows a fuel vending machine with the price, 1.999. This number is approximately equal to 2. The approximation becomes exact when you have an infinite number of nines, as indicated in the equation above.

## Step 11: Using the Formula for "pure" Repeating Decimals

The trick here is to expand the decimal number as a the sum of its integer part, the non-repeating part, and whatever is left over. The whatever-is-left-over part will contain a pure repeating decimal times a power of 10. Then I use the formula for a pure repeating decimal to find the equivalent fraction f(p) = p/(10n-1) Then I substitute that fraction back into the previous expression.

Let's revisit one of the repeating decimals from Step 7.
`r = 3.45`
From inspection this is an impure repeating decimal because it has a non-repeating part. It also has an integer part, which is another reason why it is not pure.

Expand this decimal as a the sum of its integer part, the non-repeating part, and whatever is left over.
`r = 3 + 0.45 + 0.00`
The middle term is equal to 45/100 and the last term is equal to (1/100)*0. 
`r = 3 + 45/100 + (1/100)*0.`
Using the formula for a pure repeating decimal, f(p) = p/(10n - 1), find 0.  =6/9 =2/3. Substitute this into the previous expression and get:
`r = 3 + 45/100 + (1/100)*(2/3)`
Find a common denominator for the two fractional terms, and add these together. This gives an answer in mixed fraction form.
`r = 3 + 135/300 + 2/300 = 3 + 137/300`
Convert this to an improper fraction, just to check that it is the same result as the one found in the example in Step 7.
`r = 3 + 137/300 = 900/300 + 137/300 = 1037/300`

Another example:
`r = 0.00`
This decimal has zero integer part, and non-repeating part consisting of two zeros. The repeating part is not "touching" the decimal point, so this repeating decimal cannot be pure.

However it is equal to (1/100)*0. , and 0.  = 45/99 is a pure repeating decimal.
`r = (1/100)*0. = (1/100)*(45/99) = 45/9900 = 1/220`

## Step 12: Everything Else Is Irrational

In the previous steps I have shown unto thee, two different kinds of decimal numbers: the kind that end in a repeating decimal, and the regular kind that just end, or equivalently, end in a string of trailing zeros. Both of these I was able to convert into a fraction, a ratio of two integers. It turns out this is the very definition of the so called rational numbers.

But there are stranger things out there. There are numbers whose decimal representations have an infinite number of digits, but the digits do not repeat themselves. These numbers cannot be expressed as the ratio of two integers, and this is the definition of an irrational number.

The usual suspects, pi, e, 21/2, have the following approximate decimal representations:

pi = 3.1415926535898...

e = 2.718281828459...

21/2 = 1.4142135623731...

At the end of each of these approximations I have placed a dot-dot-dot .

At the end of a the repeating decimal sequence of a rational number, the dot-dot-dot means "et cetera", which means more of the same.

However the dot-dot-dot placed here simply indicates that the sequence continues, and I don't know what it is going to do next.

You might be wondering what it is that's driving these irrational numbers to produce these unending streams of digits, and the answer is the same as it always was: big numbers constructed out of smaller numbers.

The links below can explain this more completely than I can.

http://mathworld.wolfram.com/RationalNumber.html
http://mathworld.wolfram.com/RepeatingDecimal.html
http://mathworld.wolfram.com/IrrationalNumber.html
http://en.wikipedia.org/wiki/Rational_Number
http://en.wikipedia.org/wiki/Irrational_number First Prize in the
Burning Questions Round 6.5 Participated in the
Burning Questions Round 6.5