Mail Archives: djgpp/1998/01/20/12:35:29
>I want to know how to assign a double value to an int. Like this for
>example:
Sure, use typecasting. This tells the compiler, convert from such-and-such a
format to such-and-such, if you can. If it can't it will give you a message
more or less (often less, unfortunately) to that effect.
>int hej = (PI*sin(34)/43);
int hej = (int) ( (float)PI * (float)sin(34) / (float)43 );
OR
int hej = int( float( PI ) * float( sin(34) ) / float( 43 ) );
But some of these probably don't need to be in there. But you can put them
all just to be safe. This probably is fine (note the 43.0 versus just plain
old 43):
int hej = (int) (PI * sin(34) / 43.0);
>I want the value rounded. Please help me I'm a beginner =).
I think that it gets rounded when converted from float, but remember that
when just dividing two integers nothing is rounded, or rather, it's always
rounded down.
- Calvin -s
- Raw text -