StrtoFloat

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

StrtoFloat

#1 Post by bobspencerr » 25 Jun 2013 04:59

Does anyone have a procedure or function to convert string to Float?
I cant believe it not a standard part of the program libraries.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: StrtoFloat

#2 Post by filip » 25 Jun 2013 10:25

Hi,

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;
Regards,
Filip.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: StrtoFloat

#3 Post by yo2lio » 25 Jun 2013 21:42

I attached Str2Float function.

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

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: StrtoFloat

#4 Post by filip » 26 Jun 2013 08:07

Hi,

Thanks Florin for this great addition! :)

Regards,
Filip.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: StrtoFloat

#5 Post by LGR » 28 Jul 2013 00:25

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.
If you know what you're doing, you're not learning anything.

Carlos L R
Posts: 45
Joined: 28 Jun 2013 03:22

Arm as well

#6 Post by Carlos L R » 28 Jul 2013 07:08

Compiled without problens on ARM pascal also

Post Reply

Return to “mikroPascal PRO for PIC32 General”