Get vulgar fraction from decimal

Posted in Mathematics

This is a algorithm for getting vulgar fraction from decimal fraction.

function HOD(x, y: Integer): Integer;
begin
  if (x mod y)<>0 then
    Result:=HOD(y, x mod y)
  else Result:=y;
end;

procedure Elements(St: string; var x,y: Real);
var
  Denom, i, L, Max: Integer;
begin
  Denom:=1;
  L:=Length(St);
  for i:=1 to L do
    Denom:=Denom*10;
  Max:=HOD(Denom, StrToInt(St));
  y:=Denom/Max;
  x:=StrToInt(St)/Max;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  P, Max: Integer;
  St: string;
  x, y: Real;
begin
  P:=Pos('.', Edit1.Text);
  if P<>0 then
  begin
    Label6.Caption:=Copy(Edit1.Text, 1, P-1);
    St:=Copy(Edit1.Text, P+1, Length(Edit1.Text)-P);
    Elements(St, x, y);
    Label2.Caption:=FloatToStr(x);
    Label4.Caption:=FloatToStr(y);
  end
  else
  begin
    Label6.Caption:=Edit1.Text;
    Label2.Caption:='1';
    Label4.Caption:='1';
  end;
end;