Thread: Always rounding up

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    Always rounding up

    I'm trying to figure how much paint someone will need. I have this right here:

    int gallonsPaint = 0;
    double paintCost = 0;

    gallonsPaint = (int)totalFootage / SQUARE_FOOTAGE;



    Anything over 0 should be 1. If gallonsPaint is over 1 then I want to output 2. Anything over 2 should be 3. And so on. What should I do?

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    This is kind of a hack, but you could always do something similar to:
    Code:
    gallonsPaint=int(totalFootage/SQUARE_FOOTAGE+.999999999);
    Thats if totalFootage or SQUARE_FOOTAGE is a double or float. If not, then cast them as one.

    [edit] I'm pretty sure the math header has a ceiling function you could look into as well [/edit]
    Last edited by PJYelton; 10-21-2004 at 12:05 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option is to use function ceil().
    Code:
    #include <cmath>
    .
    .
    gallonsPaint = static_cast<int> (std::ceil(totalFootage / SQUARE_FOOTAGE));

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