Click here to Skip to main content
15,881,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, was wondering if anyone can inform me how to round up in C++.
Basically making a paint estimator and for example

1 ltr of paint covers 8 square meters

float ceiling = 18.7;

ceiling = 18.7/8;

so now ceiling will be 2.3375

but you cant buy 2.3375 litres worth of paint you need 3, so I wana know how I could program it so that I can round 2.3375 or whatever the data input might be up to the next whole number.
Posted

It looks like you want to know how many lots of a certain size are in a particular number. I'd be tempted to try something like:

double round_up_to_nearest_lot( double quantity, double lot_size )
{
    unsigned lots = unsigned( quantity / lot_size );
    if( (lots * lot_size) < quantity ) lots++;
    return lot_size * lots;
}


The key here to work out a low order estimate of how many lots minimum you need to cover a particular quantity then check that it's enough. If not bump the number by one and use that many lots.

Just re-reading your problem a bit.. you actually want to find the number of lots (i.e. cans of paint), so you could do something like:

unsigned number_of_lots_in_quantity( double quantity, double lot_size )
{
    unsigned lots = unsigned( quantity / lot_size );
    if( (lots * lot_size) < quantity ) lots++;
    return lots;
}


and you'd call it along the lines of:

unsigned cans_of_paint_required( number_of_lots_in_quantity( area_to_cover, size_of_can ) );


Anyway, hope that's something approaching what you want and they way I've gone about it is understandable. Note that both those functions are going to be rubbish if the number of lots is greater than the size of an integer on your system.

Cheers,

Ash
 
Share this answer
 
Comments
WurmInfinity 11-Nov-10 12:33pm    
Wow that looks good, but im afraid this is my first C++ assignment and we've been given a task and I don't think the tutor has quite comprehended how difficult this might be for us learners, but I do like a challenge sooooo ok basically the float will not be static it will be a cin >> and will later be displayed as a cout <<. So far 1 litre of paint covers 8 square meters. Inputs for area have already been done so i'm looking for basically wallArea/8 to give me how much paint is needed, then I want that number to be rounded up and stored as a variable to be displayed in cout <<. Sorry i'm not good at explaining :(.
Add 0.9999 to ceiling and round that down.
 
Share this answer
 
Comments
WurmInfinity 11-Nov-10 10:33am    
Ceiling will be a user input, I just exampled it as a static float. Also have no idea how to round down help? :D
solved it by making it an int and adding +0.5f
 
Share this answer
 
Comments
Alain Rist 12-Nov-10 2:15am    
Works only for positive values: 4.99 -> 5.49 -> 5 OK, -4.99 -> -4.49 -> -4 BAD
Hi,

A correct answer must deal with negative numbers and with the precision of comparisons between double values.
#include <limits>
#include <cmath>
double Round(double val)
{
    const double eps = std::numeric_limits<double>::epsilon();
    return (val <= eps) && (val >= -eps) ? 0 : val > eps ? 
        val - floor(val) - 0.5 >= eps ? ceil(val) : floor(val) :
        val - ceil(val) + 0.5 <= -eps ? floor(val) : ceil(val);
}

A more flexible approach uses the built-in facilities of the iostream Standard Library:
#include <sstream>
#include <iomanip>
double Round(double val, size_t decimal)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(decimal) << val << std::ends;
    std::istringstream(oss.str()) >> val;
    return val;
}

cheers,
AR

Edit: Handle near zero input
 
Share this answer
 
v3
Why not just use the CRT ceil() function? That is exactly what it does.

From docs:

double ceil( double x );

The ceil function returns a double value representing the smallest integer that is greater than or equal to x.
 
Share this answer
 
Comments
Alain Rist 12-Nov-10 10:21am    
ceil(-4.99) == -4. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900