How to Convert double to int in C?

200,137

Solution 1

I suspect you don't actually have that problem - I suspect you've really got:

double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0
// Now cast
int b = (int) a;
// Now a is 3668

What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)

I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.

Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly

3668.99999999999954525264911353588104248046875

So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.

Solution 2

main() {
    double a;
    a=3669.0;
    int b;
    b=a;
    printf("b is %d",b);
}

output is :b is 3669

when you write b=a; then its automatically converted in int

see on-line compiler result : 

http://ideone.com/60T5b


This is called Implicit Type Conversion Read more here https://www.geeksforgeeks.org/implicit-type-conversion-in-c-with-examples/

Solution 3

This is the notorious floating point rounding issue. Just add a very small number, to correct the issue.

double a;
a=3669.0;
int b;
b=a+ 1e-9;
Share:
200,137

Related videos on Youtube

ratty
Author by

ratty

I Am Software developer from india.I am working as software developer in C#.net for past 1 Year .Contact me thorugh [email protected]

Updated on July 09, 2022

Comments

  • ratty
    ratty 3 months
    double a;
    a = 3669.0;
    int b;
    b = a;
    

    I am getting 3668 in b, instead of 3669.

    How do I fix This problem? And if have 3559.8 like that also I want like 3559 not 3560.

    • Mysticial
      Mysticial almost 11 years
      That's interesting. 3669.0 is exactly representable in floating-point...
    • Cascabel
      Cascabel almost 11 years
      Should 3668.51 map to 3669 too? (Are you trying to round, or just clip numbers that are really close?)
    • Gabe
      Gabe almost 11 years
      The code is a lie. The OP has a complicated calculation that he thinks is resulting in 3669.0 but it's really resulting in slightly less.
  • Jon Skeet
    Jon Skeet almost 11 years
    There should be no problem here at all, as 3669.0 is exactly representable in binary floating point.
  • Keith Thompson
    Keith Thompson almost 11 years
    Try adding something like printf("a = %.50f\n", a); to see the actual value of a.
  • Jon Skeet
    Jon Skeet almost 11 years
    The latter is much more likely to be the case, IMO.
  • Jon Skeet
    Jon Skeet almost 11 years
    @KeithThompson: Personally I use a bit of code I've got in C# which handles the binary value directly :)
  • curiousguy
    curiousguy almost 11 years
    "converted in double" in int?
  • Jeegar Patel
    Jeegar Patel almost 11 years
    oh ya in int...sory its my mistakes
  • Del
    Del over 7 years
    This is the answer. And an excellent answer at that! My question: What happens when @Jon Skeet's score gets too big for SO? Will our appreciation for Jon Skeet eventually kill SO?

Related