How do I cut off decimal places in C without rounding?
For example if the number is 4.48
it will just show 4.4
%.1f
This should work
double d = 4.48;
d *= 10.;
int i = d;
d = (double) i / 10.;
Here is a simple way:
printf("%.1f",trunc(x*10.0)/10.0);
If your compiler supports C99, you should be able to use trunc() and friends:
float f = 4.56f;
f = truncf(f * 10.0) / 10.0;
float myval = 4.48;
float tr = ((int)(myval*10)) / 10.0;
You can (ab)use the fact that integer division truncates and doesn't round:
float original = 4.48;
int tmp = original * 10; // 44.8 truncated to 44
float truncated = tmp / 10.0; // 4.4