![]() |
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. |
Re: StrToFloat Exception bei Konvertierung mittels ',' oder
Warum die globale Variable? Und warum ist dieser Code:
Delphi-Quellcode:
im initialization der Unit und nicht direkt in der Funktion?
if SysUtils.DecimalSeparator = '.'
then wrongDecimalSeperator := ',' else wrongDecimalSeperator := '.'; |
Re: StrToFloat Exception bei Konvertierung mittels ',' oder
Zitat:
(das ist jetzt nichts, was lange braucht es neu zu sezten/berechnen, als daß man es nicht jedesmal von Neuem ermitteln könnte) |
Re: StrToFloat Exception bei Konvertierung mittels ',' oder
im initialization-Abschnitt der Unit wollte ich eigentlich schreiben.
|
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: |
Re: StrToFloat Exception bei Konvertierung mittels ',' oder
Zitat:
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; |
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.
|
Re: StrToFloat Exception bei Konvertierung mittels ',' oder
Delphi-Quellcode:
macht man nun überhauptnicht ... Exceptions (Ausnahmen) werden nicht einfach so verworfen!
except
end; wenn man da wenigstens die entsprechende Aktion da reinverlegen würde, ginge es allergings
Delphi-Quellcode:
oder laß die Exception ganz weg und nimm StrToFloatDef
except
result := defValue; end; (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