delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/20/12:35:29

Newsgroups: comp.os.msdos.djgpp
Subject: Re: round
From: frenchc AT cadvision DOT com (Calvin French)
References: <34c3be38 DOT 741112 AT nntpserver DOT swip DOT net>
MIME-Version: 1.0
NNTP-Posting-Host: ts7ip236.cadvision.com
Message-ID: <34c3efbc.0@news.cadvision.com>
Date: 20 Jan 98 00:28:44 GMT
Organization: CADVision Development Corporation (http://www.cadvision.com/)
Lines: 30
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

>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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019