Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi StrToFloat Exception bei Konvertierung mittels ',' oder '.' (https://www.delphipraxis.net/140570-strtofloat-exception-bei-konvertierung-mittels-oder.html)

mrpix777 21. Sep 2009 10:32


StrToFloat Exception bei Konvertierung mittels ',' oder '.'
 
Hallo miteinander

Habe mich immer über Exceptions bei einer Gleitkommakonvertierung geärgert, wenn als Dezimalzeichen
nicht das voreingestellte Dezimalzeichen des Systems verwendet wurde.

Diese ändert sich je nach Ländereinstellung, z.B. Deutsch(...) oder Englisch(...).
siehe: Start / Einstellungen / Systemeinstellungen / Regions- und Sprachoptionen / Regionale Einstellungen
Durch Klick auf [Anpassen] / Zahlen / Dezimaltrennzeichen kann es seperat geändert werden.

Also verwende ich statt SysUtils.StrToFloat meine eigene Konvertierungsroutine StrToFloat2.
Damit sollten alle Programme, die Gleitkommakonvertierungen enthalten, mit beliebiger Ländereinstellung funktionieren!

Delphi-Quellcode:
uses SysUtils;
var  wrongDecimalSeperator: char;    // opposite of system decimal seperator
//*********************************************************
// convert float string with dot notation to float value
// note:      wrong decimal char will be replaced system decimal char
//             to prevent an exception !!!
// input:     floatStr: string   float string
// example:   123  11.0  -2,2  +3.33  -1.2E+045  are valid float values
//---------------------------------------------------------
function StrToFloat2 (floatStr: string; defValue: float = 0.0): Extended;
var
  cv: integer;
begin
  result := defValue;
  try
    if floatStr = '' then exit;
    // replace '.' or ',' to system decimal separator
    cv := pos (wrongDecimalSeperator, floatStr);
    if cv > 0
    then floatStr[cv] := SysUtils.DecimalSeparator;
    result := SysUtils.StrToFloat(floatStr);
  except
  end;
end;
//---------------------------------------------------------
var floatValue: float;
procedure Test1;            // test float conversion
begin
  floatValue := StrToFloat2 ('1.000');
  floatValue := StrToFloat2 ('-2.22');
  floatValue := StrToFloat2 ('+3.33');
  floatValue := StrToFloat2 ('444');
  floatValue := StrToFloat2 ('1234567890123456789');
  floatValue := StrToFloat2 ('-1.2E+45');
  floatValue := StrToFloat2 ('ABCD');          // 0.0
  floatValue := StrToFloat2 ('123,456ABCD');   // 123.456
  floatValue := StrToFloat2 ('no float');      // 0.0
  floatValue := StrToFloat2 ('+123.456+ABCD'); // exception!!!
  floatValue := StrToFloat2 ('-99.99+E999');   // exception!!!
end;
//---------------------------------------------------------
initialization
  if SysUtils.DecimalSeparator = '.'
  then wrongDecimalSeperator := ','
  else wrongDecimalSeperator := '.';
  Test1;
end.

Luckie 21. Sep 2009 10:48

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
Warum die globale Variable? Und warum ist dieser Code:
Delphi-Quellcode:
if SysUtils.DecimalSeparator = '.'
  then wrongDecimalSeperator := ','
  else wrongDecimalSeperator := '.';
im initialization der Unit und nicht direkt in der Funktion?

himitsu 21. Sep 2009 10:55

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
Zitat:

Zitat von Luckie
Warum die globale Variable?

in initialization der Unit und nicht direkt in der Funktion?

Und was passiert, wenn ich wärend der Programmlaufzeit den DecimalSeparator ändere?
(das ist jetzt nichts, was lange braucht es neu zu sezten/berechnen, als daß man es nicht jedesmal von Neuem ermitteln könnte)

Luckie 21. Sep 2009 11:02

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
im initialization-Abschnitt der Unit wollte ich eigentlich schreiben.

himitsu 21. Sep 2009 11:12

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
hast du aber nicht :tongue:

im ...-Abschnitt
in (der) Initialisation


nja, zumindestens sind wir uns einige, daß es da (wie auch immer es heißt) nicht rein muß :angel2:

mrpix777 21. Sep 2009 11:18

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
Zitat:

Warum die globale Variable? Und warum ist dieser Code:
Delphi-Quellcode:
if SysUtils.DecimalSeparator = '.'
  then wrongDecimalSeperator := ','
  else wrongDecimalSeperator := '.';
im initialization der Unit und nicht direkt in der Funktion?
Ist etwas laufzeitoptimaler, da wrongDecimalSeperator nicht bei jedem Konvertierungsaufruf neu bestimmt werden muss.

Für schnelle Copy/Paste Aktionen hier die Variante ohne Verwendung einer globalen Variable...

Delphi-Quellcode:
//*********************************************************
// convert float string with dot notation to float value
// note:      wrong decimal char will be replaced system decimal char
//             to prevent an exception !!!
// input:     floatStr: string   float string
// example:   123  11.0  -2,2  +3.33  -1.2E+045  are valid float values
// see also:  SysUtils.TextToFloat
//---------------------------------------------------------
function StrToFloat2 (floatStr: string; defValue: float = 0.0): Extended;
var
  cp: integer;
begin
  result := defValue;
  try
    if floatStr = '' then exit;
    // replace '.' or ',' to system decimal separator
    if SysUtils.DecimalSeparator = '.'
    then cp := pos (',', floatStr)
    else cp := pos ('.', floatStr);
    if cp > 0
    then floatStr[cp] := SysUtils.DecimalSeparator;
    result := SysUtils.StrToFloat(floatStr);
  except
  end;
end;

Luckie 21. Sep 2009 11:28

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
Noch was, was soll der leere try-except-Block? Lass den Standardrückgabewert weg und wirf im Fehlerfall eine saubere Exception.

himitsu 21. Sep 2009 11:34

Re: StrToFloat Exception bei Konvertierung mittels ',' oder
 
Delphi-Quellcode:
except
end;
macht man nun überhauptnicht ... Exceptions (Ausnahmen) werden nicht einfach so verworfen!

wenn man da wenigstens die entsprechende Aktion da reinverlegen würde, ginge es allergings
Delphi-Quellcode:
except
  result := defValue;
end;
oder laß die Exception ganz weg und nimm StrToFloatDef
(es macht beim Debuggen keinen Spaß, wenn dann der Debugger ständig auf diese unnötige Exception reagiert)


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:10 Uhr.

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