Well... a better approach:
Code:
/* Changed 'decimalPlaces' to 'unsigned int' to avoid 'negative'
values. Notice this value can still be very big, which will cause
problems! Still. This argument is a count of decimal places, so
it doesn't make any sense using a floating point type... */
double roundUpDecimalPlaces(double x, unsigned int decimalPlaces)
{
double mag;
/* Notice: 'decimalPlaces' will be converted to double when calling pow(). */
mag = pow(10.0, decimalPlaces);
return ceil(x * mag) / mag;
}
But, of course, keep in mind that floating point is accurate, but not exact in most cases.