Edin Hajdarevic
Edin Hajdarevic

Reputation: 112

Operator not applicable to this operand type Delphi

Someone help me to fix this error please.

[Error] Unit1.pas(39): Operator not applicable to this operand type

code is:

procedure TForm1.Button1Click(Sender: TObject);
var
  k: Integer;
  broj: Real;
begin
  k := StrToInt(Edit1.Text);
  if k <= 9 then
    broj := k
  else    
    broj := (k + 10) / 2;

  if k mod 2 = 0 then
    broj := broj / 10
  else
    broj := broj mod 10; // error line

  ShowMessage(FloatToStr(broj));    
end;

Upvotes: 5

Views: 3957

Answers (2)

Triber
Triber

Reputation: 1555

You can't use mod or div with floating point types, e.g. Real. Alternatively to previous answer you can use this.

broj := Frac(broj / 10) * 10;

or simply FMod from System.Math

broj := FMod(broj, 10);

Upvotes: 7

RM.
RM.

Reputation: 2043

The mod operator needs 2 integers. broj is real (float).

Use this instead

broj := broj - Trunc(broj / 10) * 10;

Upvotes: 5

Related Questions