D>How can I convert a floating-point value to an integer in C?How can I convert a floating-suggest worth to an integer in C?This is one of the frequently asked concerns about the C language,and also it is disputed in short in theC FAQ list,asquestion 14.6.The answer tbelow says the usage an expression like(int)(x+0.5)however admits that this strategy does not occupational for negative numbers.In addition, it would be wiser to use lengthy instead ofintThe following describes the problem in even more depth.First, the answerdepends on what type of convariation is desired: truncating or rounding.On the various other hand also, it fundamentally does not depend on the floating-pointform from which you are converting - it can be float ordouble or also lengthy double. Sometimes civilization think they know that the worth of a variable of a floating-allude variable is specifically representable as an integer. You might think that the value of x is 100.0 and you simply want it kind converted to type int, with value 100. But you need to never before rely on expectations around a floating-suggest value specifically equaling to an integer. What you most likely require in fact is rounding convariation. Truncating conversionimplies that any type of 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 before a value of a floating-suggest form (float, double or lengthy double) is to be converted to an integer type. Tbelow are specific rules which define as soon as such a conversion takes area. Here we will certainly only state that convariation occurs in an assignment choose i = x wright here i is of an integer kind and x is of a floating-suggest form. Conversion also occurs, of course, in explicit kind casts choose (int) x Rounding conversion indicates that we obtain the integer which is nearemainder to the floating-suggest value, so that e.g.


You watching: Convert double to int c++


See more: T A Psycho Killer'S Grim Design What Has An Angle, Psycho Killer



See more: wow feeling a bit morose

3.9 is converted to 4. This is typically what civilization want when they ask the question we are handling. There is no straight tool (like an operator or a library function) for rounding conversion, and strictly speaking a rounding convariation is not a convariation in the very same sense that those conversions which are defined in the C traditional. For positive floating-point worths, the simplest method to achieve a rounding convariation is to use an expression choose (long) (x+0.5) however it is much better to be prepared for negative worths, even if you perform not mean them to happen. This implies that one need to usage the conditional expressionx >= 0 ? (long)(x+0.5) : (long)(x-0.5)The value of this is expression is the integer which is nearemainder to the value of the floating-allude value of x. One can of course create a macro like#specify round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) if one demands rounding conversions a lot or wishes to make code rather even more readable. Notice that this means that the rounded worth of 1.5 is 2 and also the rounded value of -1.5 is -2. You might wish to have some various other therapy for a worth which is specifically in between two integers. The issue is, yet, not incredibly essential virtually. Beware that a convariation of a floating-allude value to an integer have the right to reason overcirculation and also that many implementations give no diagnostic in such situations. Using lengthy instead ofint might (or might not) provide you a broader selection of integers, but it is still smaller sized than the selection of floating-allude numbers. (Using lengthy is recommendable.) If performance is not crucial, it is a great concept to make your regime more durable by defining (rather of the straightforward #define above) the function lengthy round(double x) assert(x >= LONG_MIN-0.5); assert(x = 0) rerotate (long) (x+0.5); rerevolve (long) (x-0.5); or, if effectiveness is important, the macro#define round(x) ((x) LONG_MAX+0.5 ?error() : ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) This requires that you have#encompass and that you have actually an error handling routinecalled error which is a duty of type lengthy.Jukka KorpelaSeptember 1ninth, 1996
Categories: Q&A