if you want to round the number, then you use "setprecision()" or the format specifier "%.f" for "printf()" where is the number of digits you want to round to. E.g., the value 1.66667 rounded to =1 decimal place will be 1.7
but what you really want to do here is truncate. this means just "chopping off" (so to speak) all digits past the desired place will just be thrown out, and the least significant remaining digit will not be rounded.
to truncate a value, then, you need to use the "floor" function (for positive values, see caveat* below). but since this returns only a whole number, you will need to first "shift" the value to be truncated a number of decimal places that you want to truncate, floor the value, then shift them back.
(note my use of the word "shift" here is not a bitwise shift, because we're talking about decimal places, not binary places -- you will have to multiply by powers of 10)
value = 1.66667;
truncValue = value * pow(10,1); // shifts dec. pt. 1 place right
truncValue = floor(truncValue);
truncValue /= pow(10,1); // shifts dec. pt. 1 place left
you can replace the '1' with a variable to allow variable truncation.
*caveat: this will not work for negative numbers. example value = -1.666667rounded to 1 decimal point would be -1.7, and truncated to 1 decimal point should be -1.6.
However, if you used "floor" on this negative value, the result would be -1.7. Therefore, negative values need to use the converse function "ceil()" which would give the expected value -1.6.
now you can make a function like so:
double truncate (double value, int places)
{
if (value>0)
{
// do the routine with "floor"
}
else
{
// do the routine with "ceil"
}
return truncValue;
}