double to int

Hi All,
I need to get a value into an int variable from a double variable, and I keep getting errors.
The line I'm using is:
intValue1=dblValue1+dblValue2;
Is this possible, or is there a way around it?
Any pointers or help would be greatly appreciated.
Thanks
conversion from 'double' to 'int', possible loss of data


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{

	int x;

	double y = 10.1;
	double z = 20.2;

	x =  y + z;  //conversion from 'double' to 'int', possible loss of data
	cout << x << endl;





	return 0;
}
If you want to disconnect the warning given by the compiler
you can use static_cast.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{

	int x;

	double y = 10.1;
	double z = 20.2;

	x =  static_cast<int>(y + z);  	

                cout << x << endl;





	return 0;
}



Last edited on
You can convert like this
1
2
double y = 3.1222;
int x = int y;//this will make it round down though 


Topic archived. No new replies allowed.