|
-
Jun 6th, 2001, 10:42 AM
#1
Thread Starter
Lively Member
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.
-
Jun 6th, 2001, 12:32 PM
#2
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 6th, 2001, 05:52 PM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|