Converting decimals to fractions

I currently have a program spitting out camera shutter speeds as decimals. I wish to convert them into the standard notation of fractions. Every fraction is always 1/[some number] if it helps. For example, 0.00098 = 1/1020. Any ideas how?

If you're already using floating point numbers in your program then just take the reciprocal of the number and convert to an integer (warning: untested code):

shutterspeed = 0.00098;
unsigned denominator = (1.0/shutterspeed) + 0.5;

Serial.print("1/"); Serial.println(denominator);

-- Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Thanks, works like a charm. I was never good with math...

Thanks, works like a charm. I was never good with math…

Then you should watch this:


Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

I think that just made it worse.