How to get the .* from a float

How can you get everything in a floating point number from after the point?

E.g. if you had the float 5.8, how could you store that as two integer numbers with values of 5 and 8?

Edit: I figured it out:
1
2
3
float my_float = 11.595;
int i = (int)float;
float j = (my_float - (float)i);
Last edited on
So if you had 5.64 you'd get 5 and 64?
Hmmm... you'd have to use modf() which I think is in stdio. Something like
1
2
3
4
5
6
int wholes = thefloat % 1;
float decimals = thefloat - (thefloat % 1);
do
{
    decimals *= 10;
} while (decimals % 1 > 0);

This is entirely theoretical speculation and probably doesn't work.
Or you could do it with string manipulation which would probably be easier. Find the decimal and just make two substrings then input it back to integer with a stringstream.
Sorry about the double post, there's some major lag on my end.
Last edited on
blech @ all this casting.

converting to string and back is even worse.

Use modf:

http://cplusplus.com/reference/clibrary/cmath/modf/
Thanks!
That's what I said, modf. I was using quasipseudocode.
oh whoops. Yeah you did say that.

That's what I get for not reading. Haw.
Topic archived. No new replies allowed.