
-----------------------------------
sockoo
Thu Feb 08, 2007 7:53 pm

rounding float value
-----------------------------------
i need to round a float value. 

i have an assignment where i am required to paint a room. but depending on the dimensions of the room i may end up with lets say 200.4 cans of paint required.

is there a way i could figure out if the decimal point is < than 0 than round up, else !< 0 do nothing.

i thought of using the % , but soon realized i was way on the wrong track.

i also thought of converting the float value into an INT value, but than i would loose the whole decimal point and not give correct values or numbers.

i could also convert to int and than add one but than if the number is even and i add one its not giving me a good result.

ex: 

200 + 1 = 200 

it should be 200, there is no decimal.

What i need

Ex:

200.4531
Oh the decimal is < than 0 
201 cans is needed.

I hope some one is able to help me or give me advice.

Thank you in advance and i hope i have supplied enough information.

-----------------------------------
ericfourfour
Thu Feb 08, 2007 8:58 pm

RE:rounding float value
-----------------------------------
Do you mean a function that rounds up? I'm sure there is a ceil function in one libraries. Try math. Search the msdn if you can't find it.

-----------------------------------
sockoo
Fri Feb 09, 2007 10:25 am

Re: rounding float value
-----------------------------------
i'v heard of ceil, the only problem is my teacher doesnt want us to go above and beyond what he hasnt taught us.

therefore he hasnt taught us ceil so i cannot use it.

i was more refering to some odd or obscure way i could figure it out. But i guess there is no way to figure that out except for using the ciel function or unless someone else knows of another way

please post anything, all and any help is greatly appriciated.

thank you

-----------------------------------
wtd
Fri Feb 09, 2007 2:14 pm

RE:rounding float value
-----------------------------------
In that case, without knowing exactly what you have learned, we cannot help you.

-----------------------------------
r.3volved
Fri Feb 09, 2007 2:32 pm

RE:rounding float value
-----------------------------------
Look into the math.h library
...Specificly round, ceiling and floor

Programming is all about going above and beond what you already know. Whats the point in coding if you're just solving a problem that's already been given a solution? 

MSDN is a great resource for standard library definitions and examples (though their examples tend to be poor at best) for the whole Visual Studio suite.

Programming is about expanding your knowledgebase and being able to solve problems on your own without relying solely on what's been taught to you. Use the basis of your teachings as a stepping stone into self-learning.

-----------------------------------
OneOffDriveByPoster
Fri Feb 09, 2007 2:50 pm

Re: RE:rounding float value
-----------------------------------
Programming is all about going above and beond what you already know. Whats the point in coding if you're just solving a problem that's already been given a solution? 

You can also learn by figuring out the solution from the tools you already have.

Hint:  you can compare the float value and the truncated value.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Fri Feb 09, 2007 3:19 pm

RE:rounding float value
-----------------------------------
Can I just ask what you mean by the decimal is < 0?  

Here you hae 2.4 which you want to be 3.  What if it was 2.6, would it still be 3?  So in that case if there is ever a decimal you automatically round up?  

The easiest way to do that is to put it in a string and compare say 
string1("0")
string2("2.345")
for (int i=0; i < string2.length(); i++)
{
    if (string2[i].equals(".")
    {
          i++;
          if (string2[i].equals("0"))
            //etc....
    }
}

EDIT:  actually that is not the easiest way but it is a way that can work.

-----------------------------------
ericfourfour
Fri Feb 09, 2007 3:28 pm

RE:rounding float value
-----------------------------------
This is exactly what he is looking for: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT_ceil.asp.

-----------------------------------
ownageprince
Fri Feb 09, 2007 7:22 pm

Re: rounding float value
-----------------------------------
int Round(float x)
/*Returns x rounded to the nearest integer
  Pre:x>=0
  Post: rounded x returned				*/
{
	float FractionalPart=x-int(x);
	if(FractionalPart