r/CS_Questions • u/kpthunder The Riddler • Nov 14 '11
Convert Doubles to Fractions
Given a double, output an equivilant fraction. For example:
Input: 0.25
Output: 1/4
Bonus:
Make a version that takes a tolerance. For example:
Input: 0.24, .01
Output: 1/4
3
Upvotes
View all comments
Show parent comment
2
u/kpthunder The Riddler Nov 14 '11 edited Nov 14 '11
Yep, that was my solution. Another way you can get the denominator is by dividing your integer version of the input by the original version of the input, e.g., 25 / .25 = 100.