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
Converting Decimals to Fractions
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted May 14, 2007, 11:18 AM EDT
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.
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/3Colin Adan13 pointsPosted May 15, 2007, 12:27 AM EDT
Very many thanks indeed
Regards,
Colin
Colin Adan13 pointsPosted May 15, 2007, 10:48 AM EDT
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
Mike GoldRank #50 · 30.6K pointsPosted May 17, 2007, 7:15 AM EDT