I want to calculate the lower limit bound for given price(2.65 * 0.9)
2.65 * (1 - 0.1) = 2.385
then round it to 2 decimal places, the result 2.385 -> 2.39
round(2.385) = 2.39
but when I use C++ to implement above:
double pclose = 2.65;
double range = 0.1;
double lmtdown = pclose * (1 - range);
double lmtdownAfterRound= std::round( lmtdown * 100 ) / 100;
printf("pclose=%.2f, range=%.2f, limit down before round=%f,\n", pclose, range, lmtdown);
printf("limit after round=%f\n", lmtdownAfterRound);
but the result printed is 2.38!!!!!! instead of 2.39 I wanted
pclose=2.65, range=0.10, limit down before round=2.385000,
limit after round=2.380000
I have tried anohter round method with std::floor:
lmtdownAfterRound= std::floor(lmtdown * 100 + 0.5) / 100;
printf("limit after round =%f\n", lmtdownAfterRound);
the result is still 2.38:
limit after round with floor=2.380000
is there something wrong in my c++ code, How can I get the correct round value?