Shop OBEX P1 Docs P2 Docs Learn Events
string to float — Parallax Forums

string to float

s2jesses2jesse Posts: 62
edited 2008-07-24 05:10 in Propeller 1
Is there a simple object etc or a simple meathod to do· a string to float...
I've used fromString etc but it doenst seem to work with my float math

As a simple example im taking the knots string·from my gps·and converting it to mph but i cant seem to get a float im guesing it is only converting it out to an int.

Thanks Sorry for my continued newbness...

Jesse
·

Comments

  • AleAle Posts: 2,363
    edited 2008-07-22 07:40
    Will be a good idea if you can get back the number converted and compare it with what scanf or other simular function will produce. An example of the numbers your gps module gives could also help to determine why (if) it is failing.
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-22 13:03
    Try the string to float object. The new one works pretty good.
  • s2jesses2jesse Posts: 62
    edited 2008-07-23 00:09
    I·searched the obex for string to float i didnt see the object you speak of.. am i blind?
    Thanks
    Jesse
  • ratronicratronic Posts: 1,451
    edited 2008-07-23 00:25
    It's called "FloatString" in the obex.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Fix it, if it ain't broke·
    D Rat


    Dave Ratcliff· N6YEE
  • s2jesses2jesse Posts: 62
    edited 2008-07-23 05:08
    From what i can tell that does the opposite of what "think" i need heres an example...

    '===== Speed MPH =====
        term.out($0D)                                     'line break   
        term.str(string("MPH:"))
        term.str(sense.mph) 
    
    

    PUB mph | k, m
      k := gps.speed     
      m := fm.FMul(k, 1.1508)
         
      return fs.FloatToFormat(m,6,2)
    


    k is a string like 0.14 (ive checked it)
    m is always· coming through as "··· 0.0"

    if i manually set k to something like··· ·k := 4.5 it works
    the result works.· Im having hard time grasping the simple basics of how these variables work. i dont get how the prop stores a float in a long vs a string etc.






    Post Edited (s2jesse) : 7/23/2008 5:13:27 AM GMT
  • LeonLeon Posts: 7,620
    edited 2008-07-23 05:17
    It uses 32-bit IEEE floating-point format:

    http://en.wikipedia.org/wiki/IEEE_754

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • s2jesses2jesse Posts: 62
    edited 2008-07-23 05:36
    I see thanks

    Hrm well it is clearer to me that I do need to convert the string the gps object is returning to a float.
    Ive used numbers.spin to convert to ints etc but i just don't see any usermade objects that will convert to float..

    Any last insight before I dive in and poorly attempt to reinvent the wheel. [noparse]:)[/noparse]
  • AribaAriba Posts: 2,685
    edited 2008-07-23 09:33
    Hello Jesse

    here is a simple String to Float conversion:
    PUB StrToFloat(strptr) : flt | int,exp,sign
      int := exp := sign := 0
      repeat strsize(strptr)                   'string to integer
        case byte[noparse][[/noparse]strptr]
          "-":      sign~~
          ".":      exp := 1
          "0".."9": int := int*10 + byte[noparse][[/noparse]strptr] - "0"
                    if exp
                      exp++                    'count dec places
          other:    quit
        strptr++
      if sign
        int := -int
      flt := f.FFloat(int)
      if exp
        repeat exp-1
          flt := f.FDiv(flt,10.0)              'adjust float
    
    


    This function supports no scientific format (i.e. 1.23e6), and max 9 decimal digits in the string.

    Attached is a little Demo.

    Andy
  • grasshoppergrasshopper Posts: 438
    edited 2008-07-23 17:00
    Perhaps you can post some more of the code so we can see the problem. Also you have to make sure that both numbers are the same data type i.e. "floating point" to multiply them together.

    Post Edited (grasshopper) : 7/23/2008 5:05:19 PM GMT
  • s2jesses2jesse Posts: 62
    edited 2008-07-23 23:01
    grasshopper,,
    basically that was the problem i needed to know how to make them both floating point... hence the question how do i convert a string to a float... but i think ariba has graciously supplied me with the answer / code... i have yet to try it out but when i can ill give it a shot... thanks all
  • s2jesses2jesse Posts: 62
    edited 2008-07-24 05:10
    Ariba

    yep thanks worked like a charm!
Sign In or Register to comment.