A common trick is to do it with maths:
value = round( value * 1000.0 ) / 1000.0;
Where round
will handle negative and positive values correctly… Something like this (untested):
inline double round( double val )
{
if( val < 0 ) return ceil(val - 0.5);
return floor(val + 0.5);
}
You’ll still want to set the decimal places to 3 during output, due to floating point precision problems.