# rounding floats

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

## Recommended Posts

how do i round a float to the nearest whole number?

##### Share on other sites
floor( num ) - Rounds down
ceil( num ) - Rounds up
(int)(num + 0.5f) - Rounds down below .5, up otherwise

##### Share on other sites
    inline float round( float x )    {        return floorf( x + 0.5f );    }
BTW, "(int)(num + 0.5f)" does not work for negative numbers.

##### Share on other sites
the floor and ceil function return a double.. is it still rounded? how do i make it a long. would i just do:

(long)ceil(mydouble);

##### Share on other sites
Quote:
 Original post by EvilKnuckles666the floor and ceil function return a double.. is it still rounded? how do i make it a long. would i just do:(long)ceil(mydouble);

Yes and yes. It will return the rounded value as a double and a simple cast like you have here will make it a long.

##### Share on other sites
inline float round(float num)
{
return floorf(num + 0.5f);
}

floorf() returns a float, floor() returns a double.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 40
• ### Forum Statistics

• Total Topics
634130
• Total Posts
3015705
×