Recent

Author Topic: How to get float numbers from string list and sum them all?  (Read 2384 times)

5tr1x

  • Newbie
  • Posts: 5
How to get float numbers from string list and sum them all?
« on: April 21, 2020, 09:52:22 pm »
well, sorry for the hazing but: how can i get the numbers with positive and negative values ​​from a text list and sum them all

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: How to get float numbers from string list and sum them all?
« Reply #1 on: April 21, 2020, 09:59:48 pm »
Not very robust, since it requires all lines to contain a correctly formatted number, but somethig like this should do the trick:
Code: Pascal  [Select][+][-]
  1. function AddEmUp(const AList: TStringList): Double;
  2. var
  3.   sv: String;
  4. begin
  5.   Result := 0.0;
  6.   for sv in AList do
  7.     Result :=  Result + StrToFloat(sv);
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

5tr1x

  • Newbie
  • Posts: 5
Re: How to get float numbers from string list and sum them all?
« Reply #2 on: April 22, 2020, 03:09:57 am »
thank you lucamar :D, I think that this example function that you recommend can serve me well. Greeting

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1594
    • Lebeau Software
Re: How to get float numbers from string list and sum them all?
« Reply #3 on: April 22, 2020, 07:53:38 pm »
how can i get the numbers with positive and negative values ​​from a text list and sum them all

The same question was asked in the Embarcadero Delphi forum yesterday (I assume by you?), and the same answer given here was given there, too - use StrToFloat().
« Last Edit: April 22, 2020, 07:55:21 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 19258
  • Glad to be alive.
Re: How to get float numbers from string list and sum them all?
« Reply #4 on: April 22, 2020, 08:59:42 pm »
how can i get the numbers with positive and negative values ​​from a text list and sum them all

The same question was asked in the Embarcadero Delphi forum yesterday (I assume by you?), and the same answer given here was given there, too - use StrToFloat().
Actually, use TryStrtoFloat when reading from a text file. That is safer. See the manual (Delphi compatible): https://www.freepascal.org/docs-html/rtl/sysutils/trystrtofloat.html
Alternatively, use one of the helpers for simple types:
yourstring.ToSingle
yourstring.ToDouble
yourstring.ToExtended.

Also Delphi compatible after XE3, I believe.

Must include sysutils for all options.
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils;
  3. var a: string = '0.12345678';
  4. begin
  5.   writeln(a.ToDouble);
  6.   // or even
  7.   writeln('0.12345678'.ToDouble);
  8. end.
« Last Edit: April 22, 2020, 09:21:43 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1594
    • Lebeau Software
Re: How to get float numbers from string list and sum them all?
« Reply #5 on: April 23, 2020, 02:45:58 am »
Actually, use TryStrtoFloat when reading from a text file. That is safer.

That is a matter of opinion.  If you know the file contents are accurate, then there is no difference between TryStrToFloat() vs StrToFloat().

Alternatively, use one of the helpers for simple types

Those don't provide Try versions, nor do they provide support for different locale formats.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018