Decimal Rounding (Round Up only) (If statement)

Hello all, I need a little help.

I am making a program that needs to do these things:

I have a number gallons

if gallons = 3.08743
gallons now needs to be 4
if gallons = 3
then gallons stays at 3
if gallons = 43.643647
then gallons = 44
but if it is 43 then stay at 43
etc etc.

Basically i want to round up if theres a decimal but keep it the same if it's a whole number.

I need to do it in an If statement also.

thanks in advance for your help!
That if statement would be redundant, because in <cmath>, there is a function called ceil that does exactly what you want.

However, if you really need that if statement, I recommend checking if the number is less than or greater than the integer equivalent.

-Albatross
For some reason my instructor told us to use an if statement, but I never learned of ceil. Thank you it's what I have been looking for!
anyone can tell me, how to round-up a number in decimal point, example:
1005.36 > 1005.40(rounded to next 10 cents), so wats the code for tis, in declaration do i need to write anything, and where should i put the round-up code , and how use it?? im a begineer, now still learnning in college.. ayone can teach me.. my assignment needed to be hand-up next week..
hi cptntan, I'm new to this too, so forgive me if this doesn't work, but I think that you could:
multiply the input by 100
change it to int form
keep adding 1 to it until it is divisible by 10
change it back to double form
then divide it by 100 so you get an answer that makes sense

you could use code that looks something like this
theInt = static_cast<int>(money * 100);
while(theInt % 10 != 0) {
	theInt++; }
result = static_cast<double>(theInt / 100);

where money is the original input, theInt is the multiplied form of money, and result is your rounded answer
given, this code only works if your answer goes to 2 or less decimal places, but if you are dealing with money, it works perfectly.
I don't like to change it from double to int and back to double, but that is the easiest way I can think of.
Last edited on
ok, i will try and see 1st..
Topic archived. No new replies allowed.