# decimal to simplest form fraction

• 01-15-2012, 09:29 PM
MonkeyMan
decimal to simplest form fraction
I'm making a simple calculator with the ability to convert a decimal figure, like 1.25 in to 1 1/4.

However, programming and math are both flinching areas for me.

Say my calculator screen says 1.28.

I've been thinking I could use the "." as a break point and grab the real numbers that way, and count the decimals after it. That way I can make it in to a fraction, but then I can't figure out how to bring it down to simplest terms.

In theory, I could use indexOf on the "." and somehow grab the real number from before it and count the decimals after it.

Now I would have 1 28/100. Simple enough I could just divide by two, but as the decimal figure can be anything, dividing by two won't always obviously cut it.

Help a noob, would ya? :)
• 01-15-2012, 09:34 PM
JosAH
Re: decimal to simplest form fraction
If you can find a numerator N and a denominator D, divide both of them by gcd(N, D).

kind regards,

Jos