Original Post
🔒 Locked
rounding a float or double
Advertisement
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]
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]
I think that
double rint (double x) does what you want.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement