PDA

View Full Version : Removing Digital Points (for all c programes)

Quboid
Feb 20, 2007, 04:25 PM
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.

SamMiller0
Feb 20, 2007, 04:30 PM
check the printf man page

Eraserhead
Feb 20, 2007, 04:30 PM
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.

toddburch
Feb 20, 2007, 04:49 PM
I tried the round function but the compiler is not recognising it

Can we go back to this? Can you post the error message? Your code snippet?

lazydog
Feb 20, 2007, 05:52 PM
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:

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

b e n

ChrisA
Feb 20, 2007, 06:09 PM
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
Feb 20, 2007, 07:18 PM
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?