I've run into a small problem. I'm trying to remove extra decimal places in c, that will be used in calculations.
For example 67.98345, I want it to be 67.98
But not just for a printf statement. I need the double to actually be 67.98 not 67.98345
I've run into a small problem. I'm trying to remove extra decimal places in c, that will be used in calculations.
For example 67.98345, I want it to be 67.98
But not just for a printf statement. I need the double to actually be 67.98 not 67.98345
@mvp's answer is okay, but it has a minor problem. The intermediate value of the multiplication can overflow the double if x is close to the maximum double magnitude. An alternative that fixes this problem is:
double intpart, fracpart;
fracpart = modf(x, &intpart);
x_rounded = intpart + round(fractpart * 100) / 100;
This will round to the nearest value:
double x_rounded = round(x*100)/100;
For completeness, these will round down or up:
double x_rounded_down = floor(x*100)/100;
double x_rounded_up = ceil (x*100)/100;