Board index » delphi » Converting Real numbers to integers

Converting Real numbers to integers

I am currently writing a program with Delphi 2.0, and I need to make an
object automatically center when FORMCREATE is called. i've used the code
below but it keeps coming up with the error 'Incompatible types: 'Integer'
and 'real''

procedure Tmainform.FormCreate(Sender: TObject);
begin
movieintro1.left:= (mainform.width /2) - (movieintro1.width /2);
end;

so what i'm trying to do is center the object horizontally by getting the
width of the form and dividing it by 2 and taking away half of the width of
the 'movieintro1' object. is there an easier way?

 

Re:Converting Real numbers to integers


Daniel ingegneri schrieb:

Quote

> I am currently writing a program with Delphi 2.0, and I need to make an
> object automatically center when FORMCREATE is called. i've used the code
> below but it keeps coming up with the error 'Incompatible types: 'Integer'
> and 'real''

> procedure Tmainform.FormCreate(Sender: TObject);
> begin
> movieintro1.left:= (mainform.width /2) - (movieintro1.width /2);
> end;

> so what i'm trying to do is center the object horizontally by getting the
> width of the form and dividing it by 2 and taking away half of the width of
> the 'movieintro1' object. is there an easier way?

1) To convert Real to Integer use the round() or trunc() functions.

2) To do the calculation you want to do, better use the div (Integer
division) operator:

  movieintro1.left:= (mainform.width - movieintro1.width) div 2;

-Michael

Re:Converting Real numbers to integers


On Tue, 9 Nov 1999 08:55:28 +1000, "Daniel ingegneri"

Quote
<s...@ozemail.com.au> wrote:
>I am currently writing a program with Delphi 2.0, and I need to make an
>object automatically center when FORMCREATE is called. i've used the code
>below but it keeps coming up with the error 'Incompatible types: 'Integer'
>and 'real''

>procedure Tmainform.FormCreate(Sender: TObject);
>begin
>movieintro1.left:= (mainform.width /2) - (movieintro1.width /2);
>end;

>so what i'm trying to do is center the object horizontally by getting the
>width of the form and dividing it by 2 and taking away half of the width of
>the 'movieintro1' object. is there an easier way?

Try :
MovieIntro1.Left :=Round((MainForm.Width/2) - MovieIntro.Widht/2));

Other Threads