Converting string to decimal
Joms
Posts: 279
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...
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
This code is assuming you have only digits in str and no dot, sign or other characters.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pullmoll's Propeller Projects
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
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
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
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
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
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)".
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!
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:
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