Previously I have declared b as a double and I now I want to convert back to int. Here is the code:Does this mean that now b is now a int where c = b? Am I right?Code:int c = static_cast<int>(b);
Printable View
Previously I have declared b as a double and I now I want to convert back to int. Here is the code:Does this mean that now b is now a int where c = b? Am I right?Code:int c = static_cast<int>(b);
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
Quote:
Does this mean that now b is now a int where c = b?
c != bCode:double b = 3.5;
int c = static_cast<int>(b);
cout<<c<<endl; //3
cout<<b<<endl; //3.5
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.
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.Quote:
Does this mean that now b is now a int where c = b? Am I right?
Therefore, c is an int, and b is still a double.