Always rounding up

This is a discussion on Always rounding up within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure how much paint someone will need. I have this right here: int gallonsPaint = 0; double ...

  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 01: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, 12:45 PM
  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, 10: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, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21