在Object Pascal 中 ‘div’‘/’'Mod'

对不同的类型的表达式相除要选用不同的除法运算符:

/  是对浮点数做除法运算;

div 是对整数做除法运算;

mod 取模  就是取余数;

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;





var

Form1: TForm1;



implementation



{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

var

  i : integer;

  f : Real;

begin

  i := 4/3;             //这个引起编译器错误

  f := 3.4 div 2.3;  //这个引起编译器错误

end;



procedure TForm1.Button2Click(Sender: TObject);

var

  i : integer;

  f : Real;

begin

  i := 6 div 3;         //正确

  f := 3.4 / 2.3;      //正确

  ShowMessage(IntToStr(i));

  ShowMessage(FloatToStr(f));

end;



end.



你可能感兴趣的:(pascal)