There is a function included in the math.h library called modf. With this function you can do what you are trying to do.
Example:
#include <stdio.h> #include <math.h> double ftof () { double floating = 3.40, fractional, integer; fractional = modf(floating, &integer); printf ("Floating: %g\nInteger: %g\nFractional: %g", floating, integer, fractional); return fractional; }
Output:
Floating: 3.40 Integer: 3 Fractional: 0.40
Please note: it is better to use double in most cases, using a float , noteter that double consumes more memory, and then float (often 4: 8 bytes). Also, if you need more accurate output from large floating numbers, you can use printf %e instead of %g , which uses a shorter representation of the floating point number. I would use %e for accuracy after all.
source share