# Converting from double to int

• 04-13-2005
wiz23
Converting from double to int
Previously I have declared b as a double and I now I want to convert back to int. Here is the code:
Code:

`int c = static_cast<int>(b);`
Does this mean that now b is now a int where c = b? Am I right?
• 04-13-2005
misplaced
Code:

```double b = 2.5764; int c = 0; c = (int)b;  //the int tells the comiler that you MEANT to convert from a double to and int                   //and the compiler won't warn you that you are losing some precision in the conversion // c == 2 == true // b == 2.5764 == true;```

edit: actually "(int) b" means evaluate 'b' as an integer
• 04-13-2005
7stud
Quote:

Does this mean that now b is now a int where c = b?
Code:

```double b = 3.5; int c = static_cast<int>(b);   cout<<c<<endl;  //3 cout<<b<<endl;  //3.5```
c != b

In addition, you might think you have 3.0 in a double variable, but really you have 2.9999999999999, and when you cast that to an int, you will get 2, and 2 != 2.99999999.
• 04-13-2005
TheColonial
Quote:

Does this mean that now b is now a int where c = b? Am I right?
No, that statement takes the value of b, casts it to an int without changing the value of b, and stores that value in another variable c.

Therefore, c is an int, and b is still a double.