I want to split the float number to two separate part as real and non real part.
For example: if x = 45.678
, then my function have to give real= 45
and non_real=678
. I have tried the following logic.
split ( float x, unsigned int *real, unsigned int *non_real)
{
*real = x;
*non_real = ((int)(x*N_DECIMAL_POINTS_PRECISION)%N_DECIMAL_POINTS_PRECISION);
printf ("Real = %d , Non_Real = %d\n", *real, *non_real);
}
where N_DECIMAL_POINTS_PRECISION = 10000
. It would give decimal part till 4 digits, not after.
It works only for specific set of decimal point precision. The code is not generic, it has to work for all floating numbers also like 9.565784
and 45.6875322
and so on. So if anyone could help me on this, it would be really helpful.
Thanks in advance.