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

Converting string to decimal

JomsJoms Posts: 279
edited 2010-05-11 03:03 in Propeller 1
I thought I have done this before, but I am trying to convert a string to a decimal number.· It displays (debugs) properly, but having problems converting it to a decimal number that I can do calculations with.

I attached the file I am working with (Motor_Minder_Test).· You will see where I have tried a few things, but getting very strange output numbers and 'garbage text'.

Thanks...




As a seperate question / rant, does anyone know where to find a search function for this forum that works?!?· Parallax can make a ton of great products, but just can't fix the search for some reason.· I think most of my questions would be answered if I could just search for them, as I am not the first person to do this...· Sorry, but I feel better now...

Comments

  • pullmollpullmoll Posts: 817
    edited 2010-05-09 00:17
    This is how you convert to decimal:
    PUB str2dec(str) | index
      result := 0
      repeat index from 0 to strsize(str)
        result *= 10
        result += byte[noparse][[/noparse]str][noparse][[/noparse]index] - "0"
    
    


    This code is assuming you have only digits in str and no dot, sign or other characters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pullmoll's Propeller Projects
  • JomsJoms Posts: 279
    edited 2010-05-09 00:26
    Ok, I am just working on trying this. I do have a 'dot' in the number sometimes, and sometimes the number of digits changes.

    Could I do a if/then to ignore the "." ?

    EDIT -·· I am getting a '788212' decimal number when I would expect to get around a 60-70 number.· Could this be because of the "."?

    Post Edited (Joms) : 5/9/2010 12:35:34 AM GMT
  • pullmollpullmoll Posts: 817
    edited 2010-05-09 07:47
    Joms said...
    Ok, I am just working on trying this. I do have a 'dot' in the number sometimes, and sometimes the number of digits changes.

    Could I do a if/then to ignore the "." ?


    EDIT - I am getting a '788212' decimal number when I would expect to get around a 60-70 number. Could this be because of the "."?

    Yes, if you got a fraction after the dot, you can't express this in integers. You would have to quit the repeat when you encounter a dot. Put this before the result *= 10
      if byte[noparse][[/noparse]str][noparse][[/noparse]index] == "."
        quit
    
    


    You have no rounding of the real number then, though. If you need that, you would have to check the digit after the dot being 5-9 and add 1 to the result in that case. As it is now, the function will truncate the decimal fraction part.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pullmoll's Propeller Projects

    Post Edited (pullmoll) : 5/9/2010 7:54:31 AM GMT
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-05-10 20:01
    Hello Joms,

    I created a custom search with google especially for the propeller-forum

    Search the Parallax Propeller-Forum

    This should work pretty good.

    the FullDuplexSerialPlus-object contains a method StrToDec

    PUB StrToDec(stringptr) : value | char, index, multiply
    
        '' Converts a zero terminated string representation of a decimal number to a value
    
        value := index := 0
        repeat until ((char := byte[noparse][[/noparse]stringptr][noparse][[/noparse]index++]) == 0)
           if char => "0" and char =< "9"
              value := value * 10 + (char - "0")
        if byte[noparse][[/noparse]stringptr] == "-"
           value := - value
    
    



    I did not test it personally if it can handle a fraction-point.
    From a quick look I guess not but that's something you can test on your own.

    But it handles negative sign

    best regards

    Stefan
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-05-10 21:24
    I looked at your program and it contains the line "RPM:=FS.FloatToString(Frpm)".· This converts the floating point number in Frpm to a string, and stores the address of the string in RPM.· You can then print out the string with debug.str(RPM).· You do not need the extra code you added to convert to a string.· I would suggest removing the·extra code that references test2 because·it is not addressing the string correctly.

    Dave
  • JomsJoms Posts: 279
    edited 2010-05-10 22:50
    Stefen, AMAZING! I used to have the bookmarked, but the page I was linking to is no longer there... I am saving this as a short cut to my desktop...thanks a ton! And on the StrToDec included with the FDS+ object, I didn't know it was there, but doesn't look like it would work exactly as it should.

    Dave, I think you're asking why I even do the step of converting it to a decimal anyways and not just debug it like you suggestion you made. I need to do calculations of the RPM's and needed a decimal for the formula I put together. Basically the project doesn't just debug the RPM but detects the RPM of a roller, and I am trying to tell how many feet-per-minute is going over the roller.

    Result:

    I did get the suggestion that pullmoll made to work after a slight modification. I started having another problem that if the number ended in something that wasn't a ".", say a number like 500 even, it would return a four digit number around 4952 or something like that. After some work, I figured out I had to put a "-1" behind the "repeat index from 0 to strsize(str)".

    PUB str2dec(str) | index
      result := 0
      repeat index from 0 to (strsize(str) - 1)
        if byte[noparse][[/noparse]str][noparse][[/noparse]index] == "."
          quit
        result *= 10
        result += byte[noparse][[/noparse]str][noparse][[/noparse]index] - "0"
    
    



    Thanks a ton for all the help with this.... I am sure I will have more questions in this project, it is the first time I am adding a PINK modual to any of my projects, but very impressed so far!
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-05-11 03:03
    Joms said...

    Dave, I think you're asking why I even do the step of converting it to a decimal anyways and not just debug it like you suggestion you made. I need to do calculations of the RPM's and needed a decimal for the formula I put together. Basically the project doesn't just debug the RPM but detects the RPM of a roller, and I am trying to tell how many feet-per-minute is going over the roller.
    Joms,
    It was unclear to me what you were doing with test2.· You don't need to convert to decimal when you already have the data in fixed point and floating point.· Why would you need to convert the string back to a number when you can just·work with the fixed or·floating point versions?· A snippet of your code is as follows:
           Fwidth := F.FFloat(period)
           Fwidth := F.FDiv(F1, Fwidth)
           Frpm := F.FMul(Fwidth, F60000)
           RPM:=FS.FloatToString(Frpm) 
            Repeat i from 0 to strsize(rpm)         ' TRY CONVERTING TO DECIMAL 
              test2[noparse][[/noparse]i] := rpm[noparse][[/noparse]byte[noparse][[/noparse]rpm++]]          ' TRY CONVERTING TO DECIMAL
           
            If s:=Debug.rxcheck    
              CASE s                                      
                  49   : Debug.str(RPM)
                    Debug.tx(13)
                    Debug.str(string(test2))        ' TRY CONVERTING TO DECIMAL     
                    Debug.tx(13)                     
    
    

    You can perform further calculations using period,·Fwidth or Frpm.· I was just pointing out that the operations you're doing with test2 are superfluous.· Even if it was needed the·expressions rpm[noparse]/noparse]byte[noparse][[/noparse]rpm++ and string(test2) are incorrect.
    Dave

    Post Edited (Dave Hein) : 5/11/2010 3:29:33 AM GMT
Sign In or Register to comment.