FormatFloat (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses two buttons, a string grid, and a text edit on a VCL form application. Rename one of the TButtons to ConversionButton and the other one to AlterCellButton.

When the conversion button is clicked, the values across the top of the string grid are converted using the formats along the left side.

Select a cell and use the Alter Cell button and text edit to change the column value to convert.

Code

// Alter cell
procedure TForm1.AlterCellButtonClick(Sender: TObject);
begin
  StringGrid1.Cells[StringGrid1.Col, 0] := Edit1.Text;
end;

procedure TForm1.ConversionButtonClick(Sender: TObject);
var
  X, Y: Integer;
begin
  for X := 1 to StringGrid1.ColCount - 1 do
  begin
    for Y := 0 to StringGrid1.RowCount - 1 do
    begin
      StringGrid1.Cells[X, Y] := FormatFloat(StringGrid1.Cells[0, Y],
        StrToFloat(StringGrid1.Cells[X, 0]));
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[1, 0] := '1234';
  StringGrid1.Cells[2, 0] := '-1234';
  StringGrid1.Cells[3, 0] := '0.5';
  StringGrid1.Cells[4, 0] := '0';
  StringGrid1.Cells[0, 1] := '';
  StringGrid1.Cells[0, 2] := '0';
  StringGrid1.Cells[0, 3] := '0.00';
  StringGrid1.Cells[0, 4] := '#.##';
  StringGrid1.Cells[0, 5] := '#,##0.00';
  StringGrid1.Cells[0, 6] := '#,##0.00;(#,##0.00)';
  StringGrid1.Cells[0, 7] := '#,##0.00;;Zero';
  StringGrid1.Cells[0, 8] := '0.000E+00';
  StringGrid1.Cells[0, 9] := '#.###E-0';
  Edit1.Text := '0';
end;

Uses

Personal tools
Assistance