Rounding double values in C++ like MS Excel does it
问题 I've searched all over the net, but I could not find a solution to my problem. I simply want a function that rounds double values like MS Excel does. Here is my code: #include <iostream> #include "math.h" using namespace std; double Round(double value, int precision) { return floor(((value * pow(10.0, precision)) + 0.5)) / pow(10.0, precision); } int main(int argc, char *argv[]) { /* The way MS Excel does it: 1.27815 1.27840 -> 1.27828 1.27813 1.27840 -> 1.27827 1.27819 1.27843 -> 1.27831 1