This is just using whole-number rounding to achieve the trick that we all learned at school for cutting off at some number of decimal places: take the first N digits after the decimal, and round the right-most one up or down.
If you think about this in rounding 12.3456 to 2 decimal places, you naturally expect the result to be 12.35, because the '4' is the right-most of the two digits and gets rounded up by the '5' that follows.
Now, to achieve this with math we utilize floor
to achieve the rounding (actually you can utilize std::round
instead). But that will take us to a whole number and we'll lose all the fractional part.
To avoid that, we first multiply by 100, moving all the interesting parts into the whole number realm:
1234.56
If you round this number to the nearest whole value with either std::floor(x+0.5)
or std::round(x)
, you then get:
1235.0
Finally, dividing this by 100 gives you the number rounded (yes, remember we rounded it) to two decimal places:
12.35
Hopefully you now see what's going on with the call to pow
. By raising 10 to the power of nPrecision
, we get a scaling factor that will provide that many decimal places after rounding, when using this trick. In this case, we wanted 2, and pow(10,2)
is 100.
I took the liberty of cleaning up your function for readability:
double RoundDouble(double doValue, int nPrecision)
{
double scale_factor = pow(10.0, static_cast<double>(nPrecision));
return std::round(doValue * scale_factor) / scale_factor;
}