Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Delphi alternative zu strtod()? (https://www.delphipraxis.net/189497-delphi-alternative-zu-strtod.html)

Whookie 19. Jun 2016 12:05

Delphi alternative zu strtod()?
 
Ich bin gerade bei der Übersetznung eines Lexers von C nach Pascal und dort wird strtod() bzw strtod_l() verwendet um Zahlen aus Formeln zu wandeln.

Code:
double strtod(const char *nptr, char **endptr);
double _strtod_l(const char *nptr, char **endptr, _locale_t locale);
Ich habe nun nichts gefunden was dem in Delphi entspricht (TextToFloat benötigt bereits genaue Informationen über den zu wandelnten String).

Vielleicht gibts ja hier jemand der eine alternative dafür kennt?

Horst0815 19. Jun 2016 13:08

AW: Delphi alternative zu strtod()?
 
StrToFloat,TryStrToFloat

sollte doch gehen

Whookie 19. Jun 2016 13:43

AW: Delphi alternative zu strtod()?
 
Zitat:

Zitat von Horst0815 (Beitrag 1340542)
StrToFloat,TryStrToFloat

leider passt das von der Parameterliste her nicht (und liefert vor allem keinen endptr zurück).

Meine momentane Lösung sieht (nach einem Blick auf TextToFloat) wie folgt aus:

Delphi-Quellcode:
// maybe have an overloaded Version with Extented result
function StrToDecimal(nptr: PAnsiChar; Var endptr: PAnsiChar; Const AFormatSettings: TFormatSettings): Double;
Var
  LDecSep: AnsiChar;
  LResult: Extended;
  LSaveNext: AnsiChar;
begin
  LDecSep := AnsiChar(AFormatSettings.DecimalSeparator);
  Result := 0;
  // decimal part (no sign -> its a token
  endptr := nptr;
  while (endptr^ >= #$30) And (endptr^ <= #$39) do // scan digits
    inc(endptr);

  // frac part ?
  if endptr^ = LDecSep then
  begin
    inc(endptr);
    while (endptr^ >= #$30) And (endptr^ <= #$39) do // scan frac-part
      inc(endptr);
  end;

  // exponent
  if (endptr^ = 'e') Or (endptr^ = 'E') then
  begin
    inc(endptr);
    if (endptr^= '+') Or (endptr^='-') then // exp can have a sign
      inc(endptr);

    while (endptr^ >= #$30) And (endptr^ <= #$39) do // scan exponent
      inc(endptr);
  end;

  if endptr <> nptr then // did we find a number?
  begin
    LSaveNext := endptr^; // save and replace next char
    endptr^ := #0;        // TextToFloat needs #0 terminated number
    try
      If AnsiStrings.TextToFloat(nptr, LResult, fvExtended, AFormatSettings) Then
        Result := LResult;
    finally
      endptr^ := LSaveNext; // restore string
    end;
  end;
end;
Edit: Als Frage an die C-Profis hier bleibt noch ob strtod() noch zusätzliche schreibweisen für die Zahlen unterstützt?


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:15 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz