Originally Posted by
mrpickle
Problems all solved ^^. Thank you!
For the purpose of just getting the first float from a string you can simplify the function quite heavily, here is one way to do it:
Simba Code:
function FloatInStr(Str: String): Double;
var
i:Int32;
tmp:String;
begin
while (Inc(i) <= Length(Str)) and (not (str[i] in ['0'..'9'])) do {nothing};
Dec(i);
while (Inc(i) <= Length(str)) and (str[i] in ['0'..'9']) do tmp += str[i];
if (i <= Length(str)) and (str[i] = '.') then tmp += '.';
while (Inc(i) <= Length(str)) and (str[i] in ['0'..'9']) do tmp += str[i];
Result := StrToFloatDef(tmp,0);
end;
I did a little code-golfing, heh..
We can have it return NaN instead, just to ensure that you don't think there is a float in the string in the case it's not (atm it returns 0).
Simba Code:
const NaN:Single = Single(Int32(-1));
function IsNan(n:Double): Boolean;
begin
try
Result := (n=0) and (n=1);
except
Result := True;
end;
end;
function FloatInStr(Str: String): Double;
var
i:Int32;
tmp:String;
begin
while (Inc(i) <= Length(Str)) and (not (str[i] in ['0'..'9'])) do {nothing};
Dec(i);
while (Inc(i) <= Length(str)) and (str[i] in ['0'..'9']) do tmp += str[i];
if (i <= Length(str)) and (str[i] = '.') then tmp += '.';
while (Inc(i) <= Length(str)) and (str[i] in ['0'..'9']) do tmp += str[i];
Result := StrToFloatDef(tmp,NaN);
end;
var
n:Double;
begin
n := FloatInStr('hello12.4');
WriteLn(IsNan(n), ', ', n); //A number!
n := FloatInStr('hello');
WriteLn(IsNan(n), ', ', n); //Not a Number (NaN) .. aka no number found in the str.
end.
I at least think this works in the current version of Simba..