Recent

Author Topic: [SOLVED] String to Double in Array  (Read 285 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
[SOLVED] String to Double in Array
« on: August 03, 2019, 11:02:16 pm »
I am trying to convert a string from a stringgrid into a double.


I tried StrToFloat, but it doesn't work... didn't think it would anyway.

I can't seem tofind the wiki page with all the converion syntax.

Anyone know where it is?

My Code (doesn't work)

Code: Pascal  [Select]
  1.  
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   i: integer;
  6.   Values: array[1..12] of double;
  7.  
  8.  
  9. begin
  10.  
  11.   for i := 0 to strGrd.RowCount do
  12.     Values[i]:=StrToInt(strGrd.Cells[2, i]);
  13.  
  14. end;      
  15.  
  16.  


Thanks
« Last Edit: August 04, 2019, 01:06:22 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

wp

  • Hero Member
  • *****
  • Posts: 6351
Re: String to Double in Array
« Reply #1 on: August 03, 2019, 11:29:50 pm »
Use your favourite search engine and type "lazarus strtofloat", and you'll get https://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.html.
Or put the cursor on "StrToFloat" in your source code and press F1 to open the help file.
Or just move the mouse over "StrToFloat" in your source code, wait a short time and a popup will appear to show you the syntax.

So, plenty of possibilities...

You're code will not work, however, even with the correct syntax for StrToFloat. There are several issues:
  • The rows in a StringGrid are numbered beginning with zero. The top-most row (having index 0), however, usually as the column title - which is not a number. Therefore, your i loop probably must skip the title and start with 1.
  • The end of your i-loop is StringGrid.RowCount - this is too much. Because when the first index is 0 then the last index must be RowCount-1
  • Your data array "Values" is dimensioned to hold 12 elements. What if there are more rows than that? What, if less?
The following code should work (untested, though)
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   Values: array[1..12] of double;
  5. begin
  6.   for i := 1 to 12 do
  7.     if i < strGrid.RowCount then
  8.       Values[i] := StrToFloat(StrGrid.Cells[2, i])
  9.     else
  10.       Values[i] := 0;  // Replacement value when grid has less rows than checked out.
  11. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: String to Double in Array
« Reply #2 on: August 04, 2019, 12:55:20 am »
Use your favourite search engine and type "lazarus strtofloat", and you'll get https://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.html.
Or put the cursor on "StrToFloat" in your source code and press F1 to open the help file.
Or just move the mouse over "StrToFloat" in your source code, wait a short time and a popup will appear to show you the syntax.

So, plenty of possibilities...

You're code will not work, however, even with the correct syntax for StrToFloat. There are several issues:
  • The rows in a StringGrid are numbered beginning with zero. The top-most row (having index 0), however, usually as the column title - which is not a number. Therefore, your i loop probably must skip the title and start with 1.
  • The end of your i-loop is StringGrid.RowCount - this is too much. Because when the first index is 0 then the last index must be RowCount-1
  • Your data array "Values" is dimensioned to hold 12 elements. What if there are more rows than that? What, if less?
The following code should work (untested, though)
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   Values: array[1..12] of double;
  5. begin
  6.   for i := 1 to 12 do
  7.     if i < strGrid.RowCount then
  8.       Values[i] := StrToFloat(StrGrid.Cells[2, i])
  9.     else
  10.       Values[i] := 0;  // Replacement value when grid has less rows than checked out.
  11. end;

@WP...

Yeah, I hadn't gotten up to that point of validating rows etc with checks yet.

I am just trying to get the float to work.

I add my checks after I am sure that basic values work first.

Thanks
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: String to Double in Array
« Reply #3 on: August 04, 2019, 01:06:04 am »
Okay... didn't realize that the Float has an extended method

Code that works now...

Code: Pascal  [Select]
  1.  
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4.   i: integer;
  5.   Values: array[1..12] of double;
  6.   E: Extended;
  7.  
  8.  
  9. begin
  10.  
  11.   for i := 1 to strGrd.RowCount do
  12.     if i < strGrd.RowCount then
  13.         begin
  14.           E:=StrToFloat(strGrd.Cells[2, i]);
  15.           Values[i]:=E;
  16.        end
  17.    else
  18.        Values[i]:= 0;
  19.  
  20.  
  21.  

NOTE: This code doesn't have all the checks yet
But, it just illustrated that the StrToFloat is working now.

THANKS!!!

 8)
« Last Edit: August 04, 2019, 01:08:41 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060