When I cast the double value 44.000000 to int, I get the desired value of 44.
But, When cast the same double value (44.000000) that was derived by some arithmetic operations to int, I get 43.
How can I get rid of this problem and get 44 instead of 43 in the second case?
Below is the code I used,
#include<stdio.h>
#include<math.h>
int main(){
double newd = 44.00;
printf("Simple Direct Conversion: %d\n\n", (int)newd);
double d = 100.44;
double decPart;
double fracPart = modf(d, &decPart);
printf("Original Values: %lf - %lf\n", decPart, fracPart);
printf("Multiplied by 100: %lf - %lf\n", decPart * 100, fracPart * 100);
printf("Converted to Integer: %d - %d\n", (int)(decPart * 100), (int)(fracPart * 100));
return 0;
}
and the output is,
Simple Direct Conversion: 44
Original Values: 100.000000 - 0.440000
Multiplied by 100: 10000.000000 - 44.000000
Converted to Integer: 10000 - 43