How can I convert a floating-point value to an integer in C?
How can I convert a floating-point value to an integer in C?
This is one of the frequently asked questions about the C language, and it is discussed in short in the C FAQ list, as question 14.6. The answer there suggests the use an expression like
(int)(x+0.5)
but admits that this technique does not work for negative numbers. Moreover, it would be wiser to use long
instead of int
The following explains the issue in more depth.
First, the answer depends on what kind of conversion is desired: truncating or rounding. On the other hand, it essentially does not depend on the floating-point type from which you are converting - it might be float
or double
or even long double
.
Sometimes people think they know that the value of a variable of a floating-point variable is exactly representable as an integer. You may believe that the value of x is 100.0 and you just want it type converted to type int, with value 100. But you should never rely on expectations about a floating-point value exactly equaling to an integer. What you probably need in fact is rounding conversion.
Truncating conversion means that any fractional part is discarded, so that e.g. 3.9 is converted to 3. Such a conversion is the default floating to integer conversion in C in the sense that it is applied whenever a value of a floating-point type (float, double or long double) is to be converted to an integer type. There are specific rules which describe when such a conversion takes place. Here we will only state that conversion occurs in an assignment like
i = x
where i is of an integer type and x is of a floating-point type. Conversion also occurs, of course, in explicit type casts like
(int) x
Rounding conversion means that we get the integer which is nearest to the floating-point value, so that e.g. 3.9 is converted to 4. This is usually what people want when they ask the question we are dealing with. There is no direct tool (like an operator or a library function) for rounding conversion, and strictly speaking a rounding conversion is not a conversion in the same sense that those conversions which are defined in the C standard.
For positive floating-point values, the simplest way to achieve a rounding conversion is to use an expression like
(long) (x+0.5)
but it is better to be prepared for negative values, even if you do not expect them to occur. This means that one should use the conditional expression
x >= 0 ? (long)(x+0.5) : (long)(x-0.5)
The value of this is expression is the integer which is nearest to the value of the floating-point value of x.
One can of course write a macro like
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
if one needs rounding conversions a lot or wishes to make code somewhat more readable.
Notice that this means that the rounded value of 1.5 is 2 and the rounded value of -1.5 is -2. You might wish to have some other treatment for a value which is exactly between two integers. The issue is, however, not very important practically.
Beware that a conversion of a floating-point value to an integer can cause overflow and that most implementations give no diagnostic in such cases. Using long
instead of int
may (or may not) give you a wider range of integers, but it is still smaller than the range of floating-point numbers. (Using long
is recommendable.)
If efficiency is not crucial, it is a good idea to make your program more robust by defining (instead of the simple #define above) the function
long round(double x) { assert(x >= LONG_MIN-0.5); assert(x <= LONG_MAX+0.5); if (x >= 0) return (long) (x+0.5); return (long) (x-0.5); }
or, if efficiency is crucial, the macro
#define round(x) ((x) < LONG_MIN-0.5 || (x) > LONG_MAX+0.5 ?\error() : ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
This requires that you have #include <limits.h>
and that you have an error handling routine called error
which is a function of type long
.
Jukka Korpela
September 19th, 1996