-
seems simple but not.
This looks like it would be a very simple task but is a lot harder than I thought. I have a double of a value say 2.999999. I want to cast this value into an integer and get the value 3. You would think that all you would have to do is to have an interer and make it equal to the double. This on the other hand makes the integer value 2. It automatically rounds down. I have looked in a c ouple of books and none of them mention this. Does anyone know a way to cast a double to the properly rounded int.
-
When you cast to an integer, it doesn't round down, it truncates. Same effect, different method -- it just removes the fractional portion. To round to the nearest number, add 0.5 and cast.
-
Just a suggestion
What you are getting is exactly what you are supposed to get as parksie mentioned. What I would do is have a look at the Floor and Ceiling functions although I am not sure whether the math.h has those included.