# Thread: does C++ have a round () function?

1. ## does C++ have a round () function?

I have 2 floating point numbers with different significant digits.

I want to round a floating point number from

0.832557389... to 0.83256000... so i can compare it to another double that holds the value 0.83256000... to see if they are equal. it sure would be nice if there was a floating point round function. my Microsoft help file shows rounding for other languages but not C++.

the modulo won't take a floating point number as an arguement and demote it automatically.

I can't believe there isn't a built in function to do this!! Any ideas?

thanks,
susan

2. double a = .832557389;
a-=.000005;
a*=100000000;
double b=int(a);
b/=100000000;

B should be the number you wanted to round it to. In the likely event I made a mistake on this code, just look at how I did it and use that strategy.

3. Code:
```#include <cmath>
using namespace std;

int RoundtoNearestInt(double input)
{
return (floor(input+0.5));
}```
now use tims strategy to incorporate a second parameter NumofDigitsAfterDot to round a float off

4. thanks S. C. and Tim, i'll give it a try.

I had the idea about multiplying by 100000 but i was looking at ceil() and had not considered floor(). neat trick. i like it.

now on to tim's part.
thanks,
susan

5. What about just using a delta when comparing?

double a;
double b;

// stuff

if (1.01 * a > b && .99 a < b) { // they are nearly equal
}

6. Code:
```if (abs (a - b) < .00001)
...```
ie if delta is sufficiently small.

7. Hadn't thought of the delta. I tend to be very litteral and exact was my first instinct. Thanks for pointing out another way to go. Will consider this and save for future applications.
Thanks!