简体   繁体   English

Delphi:“不兼容的整数和扩展类型”

[英]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:我目前面临无法将我的 ClientWidth 除以 2(或乘以 0.5,因为 Delphi 给我的错误消息)的问题:

[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...根据您所需的行为,使用TruncRound函数,如下所示...

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.这会切断小数点后的所有内容,结果只留下一个Integer类型。

As an alternative, you could also use div to accomplish this, which is a bit more straight-forward and does this conversion for you...作为替代方案,您也可以使用div来完成此操作,这更直接一些,并且会为您执行此转换...

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

An integer can only contain whole numbers (I'm assuming lblAusgabe.left is an integer).整数只能包含整数(我假设 lblAusgabe.left 是一个整数)。 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.小数点数字可以在 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.要将带分数的数字转换为整数,您可以使用 round() 函数对其进行四舍五入,也可以使用 trunc() 函数丢弃小数点后的部分。

Another solution would be to calculate the value you need with integers.另一种解决方案是用整数计算您需要的值。 For example:例如:

cmdFakultat.left := ClientWidth div 2-60; cmdFakultat.left := ClientWidth div 2-60;

Div divides an integer and gives you back an integer value discarding anything behind the decimal point. Div 除以整数并返回一个整数值,丢弃小数点后面的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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