简体   繁体   中英

Delphi: “Incompatible types Integer and Extended”

I currently face the problem of not being able to divide my ClientWidth by 2 (or multiply with 0.5 because of the error message Delphi is giving me:

[Error] Unit1.pas(59): Incompatible types: 'Integer' and 'Extended'
[Error] Unit1.pas(60): Incompatible types: 'Integer' and 'Extended'
[Error] Unit1.pas(61): Incompatible types: 'Integer' and 'Extended'

procedure TForm1.FormResize(Sender: TObject);
begin
 // responsive design
 with form1 do begin
 cmdFakultat.left:=0.5*ClientWidth-60;
 txtEingabe.left:=0.5*ClientWidth-60;
 lblAusgabe.left:=0.5*ClientWidth-60;
 end;
end;

end.

Use the Trunc or Round functions, depending on your desired behavior, like this...

lblAusgabe.left := Trunc(0.5 * ClientWidth) - 60;

or

lblAusgabe.left := Round(0.5 * ClientWidth) - 60;

This cuts everything off after the decimal, leaving just an Integer type as a result.

As an alternative, you could also use div to accomplish this, which is a bit more straight-forward and does this conversion for you...

lblAusgabe.left := (ClientWidth div 2) - 60;

An integer can only contain whole numbers (I'm assuming lblAusgabe.left is an integer). The value you are trying to assign to it has a decimal point. Decimal point numbers can be stored in real, double or extended values in Pascal. To convert a number with a fraction to an integer you can round it using the round() function or you can discard the part after the decimal point using the trunc() function.

Another solution would be to calculate the value you need with integers. For example:

cmdFakultat.left := ClientWidth div 2-60;

Div divides an integer and gives you back an integer value discarding anything behind the decimal point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM