how to truncate a number with a decimal point into a int? what's the function for this?

c

Solution

It truncates automatically is you assign value to "int" variable:

int c;    
c = a/b;

Or you can cast like this:

c = (int) (a/b);

This truncates it even if c is defined as float or double.

Usually truncation is not the best (depends what you want to achieve of course). Usually result is rounded like this:

c= round(a/b,0);

is more intelligent because rounds result properly. If you use linux, you can easily get reference with "man round" about exact data types etc.

Problem

The problem occurs when I do a division operation. I would like to know who to truncate a number with a decimal point into a whole number such as 2, 4, 67.

Original source