Thread: rounding up

  1. #1

    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.


  2. #2
    Registered User
    Join Date
    Feb 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.

  3. #3
    Registered User
    Join Date
    Jan 2002

    Printf can round up for you

    #include <stdio.h>
    int main(void)
    	float b = 1.90;
    	printf("%.0f",b);  // %.0f means no decimal places.
    	return 0;

  4. #4
    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)

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    (int) rounds down all the time (truncates), so this should always round up:

    float Var;
    int AnotherVar=(int)Var+1;

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Aug 2001

    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 );

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding off a double
    By C-Dummy in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 11:45 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 09:23 AM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM