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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

ChrisA

macrumors G5
Jan 5, 2006
12,907
2,152
Redondo Beach, California
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
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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.