Does anyone have a procedure or function to convert string to Float?
I cant believe it not a standard part of the program libraries.
StrtoFloat
Re: StrtoFloat
Hi,
Currently, this routine is not available in the compiler, it is on our to do list.
For now, you can use this code :
Regards,
Filip.
Currently, this routine is not available in the compiler, it is on our to do list.
For now, you can use this code :
Code: Select all
function StringToReal(var s:string[10]): real;
var
negativ : boolean;
nonzero : boolean;
ccc : byte;
n : integer;
rr : real;
dezk : real;
begin
negativ:=false;
nonzero:=false;
rr:=0;
dezk:=0;
// Parsen des String. Der String wird dabei Zeichenweise
// in eine Zahl umgerechnet
for n:=0 to strlen(s) do
begin
ccc:=s[n];
if ccc='-' then negativ:=true // Minus-Flag
else
if ccc='.' then dezk:=1.0 // Dezimalpunkt erkannt
else
if (ccc>='0') and (ccc<='9') then // Ziffer erkannt
begin
ccc:=ccc-'0'; // Convert ASCII-'2' -> 2
if (ccc>0) and (dezk>0) then // Nur wenn Nachkomma<>0 gibt es den
nonzero:=true; // Rundungsfehler, darum Flag setzen.
rr:=rr*10.0;
rr:=rr+ccc;
if dezk>0.0 then dezk:=dezk*10.0; // Komma-Korrektur pflegen
end;
end;
if dezk>0 then rr:=rr/dezk; // Komma-Korrektur;
if nonzero then rr:=rr+0.00001; // Rundungsfehler korrigieren
if negativ then rr:=-rr; // Zahl invertieren
result:=rr;
end;
Filip.
Re: StrtoFloat
I attached Str2Float function.
Enjoy!
Enjoy!
Code: Select all
function Str2Float(var data_str4 : string[17]) : real;
var txt10 : string[10];
lungime, lungime1 : byte;
pt1, pt2 : ^byte;
begin
result := 0.;
lungime := Str_Len(data_str4);
if (lungime = 0) or (lungime > 17) then exit;
lungime1 := Str_Chr(data_str4,'.');
if lungime1 = 0 then exit;
txt10 := '00000';
pt1 := @data_str4[lungime1];
pt1^ := 0;
pt1 := pt1 + 1;
pt2 := @txt10;
inc(lungime1);
while lungime1 < lungime do
begin
pt2^ := pt1^;
pt2 := pt2 + 1;
pt1 := pt1 + 1;
inc(lungime1);
end;
if data_str4[0] = '-' then result := real(Str2LongInt(data_str4)) - real(Str2LongWord(txt10))/100000.0
else result := real(Str2LongInt(data_str4)) + real(Str2LongWord(txt10))/100000.0;
end;
Last edited by yo2lio on 08 Jan 2019 09:45, edited 1 time in total.
Best regards, Florin Andrei Medrea.
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
Re: StrtoFloat
Hi,
Thanks Florin for this great addition!
Regards,
Filip.
Thanks Florin for this great addition!
Regards,
Filip.
Re: StrtoFloat
Florin,
I don't see that at Libstock. It would be more convenient if that were a package. Is there a reason that you didn't put it there?
BTW, that looks like a really nice library.
I don't see that at Libstock. It would be more convenient if that were a package. Is there a reason that you didn't put it there?
BTW, that looks like a really nice library.
If you know what you're doing, you're not learning anything.
-
- Posts: 45
- Joined: 28 Jun 2013 03:22
Arm as well
Compiled without problens on ARM pascal also