-
January 8th, 2003, 11:06 AM
#1
round to integer
I can´t find any function for doing this.Is there any fast,simple way of doing it?
Thank you.
-
January 8th, 2003, 11:17 AM
#2
For positive numbers it is simple:
Code:
double y = 1.234;
int x = reinterpret_cast<int>(y + 0.5);
For negative numbers you have to decide if you are rounding DOWN (a more negative number) or TOWARDS zero. That will change the sign....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 8th, 2003, 11:19 AM
#3
I don't know if there is any function, but an easy way to do it is this:
Code:
double number = ...;
int round = (number<0) ? (int)(number-0.5) : (int)(number+0.5);
I hope this helps you.
Last edited by irona20; January 8th, 2003 at 11:22 AM.
I am Miss Maiden... Miss Iron Maiden :-D
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
|