Skip to main content
gamedev.net
🔒 Locked

rounding a float or double

Started by amish1234 4 replies 200+ views
Original Post
amish1234
amish1234
I need a function that rounds a float or double to the nearest integer. I tried making an int = a float, but that just truncates it. Anyone know of a way to do this or a function that does it for you? Proceeding on a brutal rampage is the obvious choice.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
munkie
munkie

  
fVar = int( fVar + 0.5 );


Or I believe the "ceil" function found in math.h will round numbers as well.
Colin Jeanne
Colin Jeanne
ceil() rounds numbers up, ex ceil(1.1) = 2.0

floor() rounds numbers down, ex floor(1.9) = 1.0

You could make a rounding function like so:

double Round(double d)
{
if (fabs(d - floor(d)) < .5) //Gets the value of decimal part
return floor(d);
else
return ceil(d);
}

*EDIT made that Round function, abs() is for ints.... fabs() is for floats/doubles



I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader's Realm

[edited by - Invader X on June 14, 2002 8:38:38 PM]

[edited by - Invader X on June 15, 2002 2:55:24 AM]
smart_idiot
smart_idiot
I think that double rint (double x) does what you want.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.