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;