Skip to content

Converting Decimals to Fractions

Colin Adan

Asked May 13, 2007, 11:20 AM EDT4 answers
Share
LinkedInX
Hi everyone,

I am trying to convert decimal numbers to fractions. I did manage to get something working but then hit the problem of handling negative decimals.

Does anyone have a way of converting decimals to fractions? I need something to convert the following

decimal         convert to      fraction
1.2                                   1/5
1.36                                 4/11
2.25                                 5/4
etc etc

If you have anything I could use or better still point me towards an article that would be great. I have tried google but found nothing suitable :-(

Many thanks in advance
Colin

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    1) multiply the number by 10 until there are no more fractions. 
    2) Track the multiple
    3) Then use Euclid's gcd (greatest common denominator) algorithm on the numerator and denominator (see below) to determine what to divide the numerator and denominator by.
    C#
    function extended_gcd(a, b)
        if a mod b = 0
            return {0, 1}
        else
            temp := extended_gcd(b, a mod b)
            x := first(temp)
            y := last(temp)
            return {y, x-y*(a div b)}
     
    for recurring fractions (e.g.  .33333333)   use    recurring number   /  (base 10 number - 1) 
    
    so for .33333 this would be 3/(10 - 1) = 3/9 = 1/3    or   333/(1000 - 1) = 333/999 = 1/3
    0
  • 0
    Mike,

    Very many thanks indeed

    Regards,
    Colin
    0
  • 0
    Mike,

    I think I'm missing something.

    I think I understand what the function is doing and I understand the need to multiply to get rid of the decimalisation in the original number. But the fuction takes two parameters. The first is surely the normalised decimal, but what is the second parameter?

    Many thanks again,
    Colin
    0
  • Mike Gold
    0
    The first parameter is the larger of the numerator and denominator, the second is the smaller of the numerator and denominator.  It will return the least common denominator.  You can divide top and bottom by this number.
    0