C++: int to a double?

jguy100

Prince
Joined
Apr 3, 2003
Messages
468
Quick question for those who know C++:
Is there a quick way to convert an int to a double?
Thanks.
 

Aphex_Twin

Evergreen
Joined
Sep 7, 2002
Messages
7,474
(double)our_int_value

should turn that into a double.
 

Mise

isle of lucy
Joined
Apr 13, 2004
Messages
28,623
Location
London, UK
if "a" is an int and you want "b" to be the double, then try "b = a * 1.0;" . But i'm not really sure what you mean, or if that works.
 

Aphex_Twin

Evergreen
Joined
Sep 7, 2002
Messages
7,474
Mise said:
if "a" is an int and you want "b" to be the double, then try "b = a * 1.0;" . But i'm not really sure what you mean, or if that works.
That is compiler-dependent. (Python is fun, I know :D )

Type conversion operators are the sure way to go.

Edit:
As an excercise, do the following:
float a = 3.76, b = 2.32;
int c

Try to output:
1. a mod b
2. a/b
3. a - converted to int / b and the result converted to int
4. attribute c the values of a + b, then print c as if it were a float so that in our case a+b == c
 
Top Bottom