# Display numbers in grid problem

[ DelphiLand FAQ ] [ Delphi Tutorials -- by DelphiLand ]

Posted by Robert on October 12, 2004 at 22:33:24:

I'm using a TStringGrid to display numeric data of a matrix from a textfile. There's a problem when passing the numeric values, using FloatToStr to show them in the grid, it is that all the zeros that are
after the decimal comma are not shown in the grid.
When saving the content of matriz the problem is not observed.

Matriz^(i,j) is a pointer defined in another unit.
I would like that decimal part to be of the same longitude in all the numbers
so that the alignment has better aspect in the grid.

This is the content in a file of data:

4
4
10.0000800 22.1120000 1.0030000 2.0009000
111.0021000 2.0001000 3.3340000 -4.5620100
1.3900100 44.1111000 5.1150000 3.0001200
1.2000100 5.5555000 6.2230000 3.3308040

and this is what is observed in the grid

10.00008 22.112 1.003 2.0009
111.0021 2.0001 3.334 -4.56201
1.39001 44.1111 5.115 3.00012
1.20001 5.5555 6.223 3.330804

This is a part of the code

procedure SaveStringGrid(StringGrid: TStringGrid; const FileName:
TFileName);

var
f : Text;
i, j, dimen1, dimen2 : Integer;
begin
New(MATRIX);
AssignFile(f, FileName);
Rewrite(f);
with StringGrid do
begin
dimen1:=RowCount-1;
dimen2:=ColCount-1;

for i := 1 to ColCount do
for j := 1 to RowCount do
MATRIX^[i,j]:= strToFloat(Cells[j, i]);
end;

writeln(f,dimen1);
writeln(f,dimen2);
for i:=1 to dimen1 do
begin
for j:=1 to dimen2 do
begin
write(f,MATRIX^[i,j]);
end;
writeln(f);
end;

Close(f);
Dispose (MATRIX);
end;

procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
var
f : Text;
dimen1,dimen2,i,j,k : Integer;
temp0 : float;
begin
New(MATRIX);
AssignFile(f, FileName);
Reset(f);

for i:=1 to dimen1 do
begin
for k:=1 to dimen2 do
begin
MATRIX^[i,k]:=temp0;
end;
end;

with StringGrid do
begin

RowCount := dimen1+1; // Get number of rows and columns
ColCount := dimen2+1;

for i := 1 to ColCount do // loop through cells & fill in values
for k := 1 to RowCount do
begin
Cells[i, k]:= FloatToStr(MATRIX^[k, i]);
end;
end;
Close(f);
Dispose(MATRIX);
end;

Thanks very much

Robert

Related Articles and Replies

[ Delphi Forum -- by DelphiLand ] [ Delphi Tutorials -- by DelphiLand ]