Is this the best way to round up a float ?

• 09-11-2001
The Gweech
Is this the best way to round up a float ?
hi,

when casting a float to an int you lose the fractional part of the number . So does the code below round the float up to the next int in the most effective manner. If not could you please show how you would do it?

#include <iostream>
using namespace std;

int main ()

{

double num1 = 0;
int newnum = 0;

cout << endl << "Please Enter a float number and i Will round it up to the next whole number!"
<< endl;

cin >> num1;

newnum = num1 +1;

cout << newnum
<< endl;

return 0;
}
• 09-11-2001
Stoned_Coder
Why not just use the function that the language provides for this.... ciel() . You will find it in math.h or cmath(where its in namespace std)
• 09-11-2001
Brown Drake
This comes from VB, but ought to work in c++:

enter a float, add 0.5, then use floor() to round it out. If the float entered was x.5 or above, you'll round up. Below x.5, you'll round down.
Telling a user to enter a float, but secretly treating it as an int, will only round down. 5.999 will come out as 5, not 6.
• 09-12-2001
kitten
Code:

```inline int Round(const double var) {   return var > 0 ? (int)(var + 0.5) : (int)(var - 0.5); }```