Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BSMonkey

Help with rounding

This topic is 5242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok. I know this is really easy but I just cant find it in my book and my compiler index isnt working. So heres the question. How can I round a double to an int? like 2.6 to 3? Oh and this is for C++ using visual studio .net 2003 Thanks for the help in advance. These are the two lines where Im using it maybe there is something wrong with them. Monster->LocationY = Monster->LocationY + round( ( double )Slope->SlopeY / Slope->SlopeX ); Monster->LocationX = Monster->LocationX + round( ( double )Slope->SlopeX / Slope->SlopeY ); I dont think so though the compiler error is: error C3861: ''round'': identifier not found, even with argument-dependent lookup

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by BSMonkey
How can I round a double to an int? like 2.6 to 3?



The standard c library has ceil and floor that you can use to get a float/double that represents just the integer part. You will have to create your own round function (probably using ceil and/or floor ).


float float_number = 1.01;
float floored_number = floor( float_number );
// floored_number == 1.0
float ceiled_number = ceil( float_number );
// ceiled_number == 2.0
int int_number = static_cast( float_number );
// int_number == 1

Share this post


Link to post
Share on other sites
quote:
Original post by BSMonkey
How can I round a double to an int? like 2.6 to 3?




You can add 0.5 to the number you want to round, then cast to int.

Share this post


Link to post
Share on other sites
quote:
Original post by rypskar
You can add 0.5 to the number you want to round, then cast to int.


If you need a method that works for negative numbers:
inline double round( double x )
{
return floor( x + 0.5 );
}

Share this post


Link to post
Share on other sites
OK cool man that works greate thanks for the help. I hate it when I get stuck on this little stuff it just takes away my motivation but now I got it back so I can write a few more pages of code and hopefully I wont get stuck on something stupid

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!