How can we extract the decimal part of a floating point number and to store the decimal part and the integer part into seperate two integer variables?
You use the modf
function:
double integral;
double fractional = modf(some_double, &integral);
You can also cast it to an integer, but be warned you may overflow the integer. The result is not predictable then.
Try this:
int main() {
double num = 23.345;
int intpart = (int)num;
double decpart = num - intpart;
printf("Num = %f, intpart = %d, decpart = %f\n", num, intpart, decpart);
}
For me, it produces:
Num = 23.345000, intpart = 23, decpart = 0.345000
Which appears to be what you're asking for.
I don't see how you could put the decimal part into an integer unless you knew how many digits you wanted to keep. Could you give a few examples?
Other answers have given you how to split the whole part from the fractional part. To do what you want with the fractional part, just keep multiplying it by 10 until the fractional part of that becomes 0.
You may have to deal with overflow converting that to an integer (if you're working with doubles instead of floats).
Also, I'm not sure how rounding error might screw with this - been a long time since I did numerical analysis. And even then it was pretty much the minimum to get a good grade in some class.
I'll leave those problems and the actual implementation as an exercise for the reader. Is this homework by any chance?
If you're reading in from the console, you don't need to ever store it in a float. Just read the two parts in as integers with scanf
:
int int_part, dec_part;
scanf("%d.%d", &int_part, &dec_part);
Well, floor() or casting will get you the integer part easily, but what do you actually expect to store in the "decimal" part?
Not even including issues like floating point (im)precision, what do you want to happen when your candidate float is a repeating fraction or an irrational number?
The quick "in a nut shell" most obvious answer seems like:
#define N_DECIMAL_POINTS_PRECISION (1000) // n = 3. Three decimal points.
float f = 123.456;
int integerPart = (int)f;
int decimalPart = ((int)(f*1000)%1000);
You would change how many decimal points you want by changing the N_DECIMAL_POINTS_PRECISION
to suit your needs.
Interesting, but what about this? casting does not work properly, modf() also does not work in this situation (I just want to take integral part only!)
int main()
{
double d;
int i;
int x;
d = 10.000;
for (i=1; i< 20; i++)
{
x=(int) (d*100.00);
printf("%lf --> %lf --> %d\n", d, d*100.00, x);
d = d+0.001;
}
return 0;
}
Here was the output, please read carefully at the line: 10.010000 --> 1001.000000 --> 1000
khanhnd@debian96:~/test$ gcc -Wall -O3 rounding.c
khanhnd@debian96:~/test$ ./a.out
10.000000 --> 1000.000000 --> 1000
10.001000 --> 1000.100000 --> 1000
10.002000 --> 1000.200000 --> 1000
10.003000 --> 1000.300000 --> 1000
10.004000 --> 1000.400000 --> 1000
10.005000 --> 1000.500000 --> 1000
10.006000 --> 1000.600000 --> 1000
10.007000 --> 1000.700000 --> 1000
10.008000 --> 1000.800000 --> 1000
10.009000 --> 1000.900000 --> 1000
10.010000 --> 1001.000000 --> 1000
10.011000 --> 1001.100000 --> 1001
10.012000 --> 1001.200000 --> 1001
10.013000 --> 1001.300000 --> 1001
10.014000 --> 1001.400000 --> 1001
10.015000 --> 1001.500000 --> 1001
10.016000 --> 1001.600000 --> 1001
10.017000 --> 1001.700000 --> 1001
10.018000 --> 1001.800000 --> 1001