I was trying to extract the exact fractional part from a floating point number. I tried with this:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
printf ("The fractional part is: %f", fractional);
But the output is: 0.729996
. For this reason when I was doing this:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
int fractional_part_in_integer = ((int)(f*100)%100);
printf ("The value is: %d", fractional_part_in_integer);
It gives me 72
as output. But, I want to extract exactly 73
from the given number 254.73
. I already know how to use %.2f
during printf()
function to print upto two decimal numbers. But in my code I don't want to print the number right now. I have some calculations with that fractional part as integer form i.e. 73
.
So, my problem is how could I extract the fractional part from 254.73
so that I can get exact 73
as integer to do more calculations?