# rounding up

• 02-28-2002
Unregistered
rounding up
i just started taking cpp classes in school and i got a new assignment. in this assignment i have to calculate how much it will cost for a customers carpet(after all the input).I also must use one function in this program. so i was wondering if there was a way i could make a function round up to the next number (ie if n=1.11 round it to n=2 or something). plz include examples.

thanks :)
• 02-28-2002
...
im still pretty new to C++ myself, and usually helping others is no place for someone with my knowlege level, but still....

why not just make it simple. such as:

//...

a = x / y;
/////////////
b = x % y;
if ( b > 0 )
a = ++a;
/////////////
printf("%d", a);

thats assuming that 'a' is an int, and when you divide x by y, it rounds down for a

i dont know if that helped even in the slightest, but i figured i would try to do my part.
• 02-28-2002
Brian
Printf can round up for you
Code:

```#include <stdio.h> int main(void) {         float b = 1.90;         printf("%.0f",b);  // %.0f means no decimal places.         return 0; }```
• 02-28-2002
Unregistered
thanks for the quick replies!
But i was wondering if i could mak eit round up all the time.

(1.10=2 and 1.7=2)
• 02-28-2002
Magos
(int) rounds down all the time (truncates), so this should always round up:

float Var;
int AnotherVar=(int)Var+1;
• 02-28-2002
zMan
use ceil(...) to round up
no offense to the fine examples but .....
According to the MSDN help files... use them and you rely on this board much less...

/* FLOOR.C: This example displays the largest integers
* less than or equal to the floating-point values 2.8
* and -2.8. It then shows the smallest integers greater
* than or equal to 2.8 and -2.8.
*/

#include <math.h>
#include <stdio.h>

void main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}
• 02-28-2002
Dual-Catfish
You might also want to check
if (floor(var) == var) before you go typecasting int on your float.
Take the following example

floar var;
cin >> var;
cout >> (int)var+1;

If the user enters an integer number, casting int on it and adding 1 won't give you a desired result, as it would already be an int.