#include <iostream>
#include <math.h>
using namespace std;
double round(double dValue, int nCount)
{
double dSign = dValue < 0 ? -1 : 1;
double dPow = pow((double)10., nCount); //лучше иметь предвычисленные значения
double eps = 0.001;
return dSign * (floor(fabs(dValue) * dPow + 0.5 + eps) / dPow);
}
void test(double v, int c)
{
printf("round(%.15lf, %d) = %.15lf\n", v, c, round(v, c));
}
int main()
{
test(4.725, 2);
test(4.724, 2);
test(4.7248, 2);
test(4.7249, 2);
test(4.72498, 2);
test(4.724989, 2);
test(4.7249899999, 2);
test(4.72499, 2);
test(4.724999, 2);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWF0aC5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmRvdWJsZSByb3VuZChkb3VibGUgZFZhbHVlLCBpbnQgbkNvdW50KQp7CiAgZG91YmxlIGRTaWduID0gZFZhbHVlIDwgMCA/IC0xIDogMTsKICBkb3VibGUgZFBvdyA9IHBvdygoZG91YmxlKTEwLiwgbkNvdW50KTsgLy/Qu9GD0YfRiNC1INC40LzQtdGC0Ywg0L/RgNC10LTQstGL0YfQuNGB0LvQtdC90L3Ri9C1INC30L3QsNGH0LXQvdC40Y8KICBkb3VibGUgZXBzID0gMC4wMDE7CiAgcmV0dXJuICBkU2lnbiAqIChmbG9vcihmYWJzKGRWYWx1ZSkgKiBkUG93ICsgMC41ICsgZXBzKSAvIGRQb3cpOwp9CiAKdm9pZCB0ZXN0KGRvdWJsZSB2LCBpbnQgYykKewogICBwcmludGYoInJvdW5kKCUuMTVsZiwgJWQpID0gJS4xNWxmXG4iLCB2LCBjLCByb3VuZCh2LCBjKSk7Cn0KCmludCBtYWluKCkKewoJdGVzdCg0LjcyNSwgMik7Cgl0ZXN0KDQuNzI0LCAyKTsKCXRlc3QoNC43MjQ4LCAyKTsKCXRlc3QoNC43MjQ5LCAyKTsKCXRlc3QoNC43MjQ5OCwgMik7Cgl0ZXN0KDQuNzI0OTg5LCAyKTsKCXRlc3QoNC43MjQ5ODk5OTk5LCAyKTsKCXRlc3QoNC43MjQ5OSwgMik7Cgl0ZXN0KDQuNzI0OTk5LCAyKTsKCXJldHVybiAwOwp9
round(4.725000000000000, 2) = 4.730000000000000
round(4.724000000000000, 2) = 4.720000000000000
round(4.724800000000000, 2) = 4.720000000000000
round(4.724900000000000, 2) = 4.720000000000000
round(4.724980000000000, 2) = 4.720000000000000
round(4.724989000000000, 2) = 4.720000000000000
round(4.724989999900000, 2) = 4.720000000000000
round(4.724990000000000, 2) = 4.730000000000000
round(4.724999000000000, 2) = 4.730000000000000