Different ways to round number

Posted in Mathematics

Use Trunc function for getting integer value of number. Use Round function for getting nearest whole number. And Str procedure converts number to a string with specific representation.

procedure TForm1.Button1Click(Sender: TObject);
var
   St: string;
   Number: Real;
begin
   Number:=StrToFloat(Edit1.Text);
   Label2.Caption:='Trunc - '+FloatToStr(Trunc(Number));
   Label3.Caption:='Round - '+FloatToStr(Round(Number));
   Str(Number:4:2, St);
   Label4.Caption:=St;
end;