1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <stdio.h>
#include <math.h>
double truncate(double param, double precision)
{
double result, intpart;
modf(modf(param, &result) * (1 / precision), &intpart);
return result + (intpart / (1 / precision));
}
int main()
{
double result, dval;
dval = 1.120448;
result = truncate(dval, 0.001);
printf("\nValue %f truncated to tenth place is: %f",dval,result);
dval = 1.139124;
result = truncate(dval, 0.001);
printf("\nValue %f truncated to tenth place is: %f",dval,result);
dval = 1.194763;
result = truncate(dval, 0.001);
printf("\nValue %f truncated to tenth place is: %f",dval,result);
printf("\n");
return 0;
}
|