Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
Hey guys/girls, just a quick question. Does anyone have an idea as to what function i need to use to remove decimal points from a number in C. I tried the round function but the compiler is not recognising it (i am using mat.h library). Any help would be greatly appreciated.
THanks

ps. I am on a windows machine.
 
It's math.h ;).

One way is to multiply the number up, add 0.5 and convert to an int/long and then multiply back down, though that adds inaccuracies to your calculations.
 
I'm not sure exactly what you want to do, but if all you want to do is to convert, say, a float to the nearest int then roundf() is the right way to do it. Failing that you could do something like this:

Code:
int int_value = float_value > 0.0f ? float_value + 0.5f : float_value - 0.5f ;

b e n
 
The simplest way to round a float to an int is like this


somefunct()
{
double f = 2.3;
int i;

i = (int)(f+0.5);
}

Adding .5 and then truncating has the effect of rounding but saves having to do a test and branch
 
The simplest way to round a float to an int is like this


somefunct()
{
double f = 2.3;
int i;

i = (int)(f+0.5);
}

Adding .5 and then truncating has the effect of rounding but saves having to do a test and branch

Thats the simpliest way?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.