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? :)
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).