Followers 0

RTOS in CompactLogix

15 posts in this topic

I am converting a real that will always be x.xx (so 0.00 to 9.99 bascially). When I use the RTOS command to convert it to a string I am getting lots of extra decimal places. real 1.02 (REAL) put through the RTOS command destination is 1.01999998 (STRING) where is this coming from and what is the easiest way to make sure the string ends up with 1.02? Don't care what else I have to do even if it is another command. Just remember the destination is a STRING so the main problem is you can't really do any math on the string to round or truncate I don't think. I'm stumped for today

Share on other sites
The wonders of floating point math :) The easiest fix would be to set your string's length to 4 after doing the RTOS. This will truncate all but two decimal places (as long as your whole value is less than 10), which could be a problem because you aren't rounding.

Share on other sites
Don't know if this would work or not, but might be worth trying. Multiply (MUL) your float by 100 with a DINT as the destination, then divide (DIV) by 100 with float as destination. Then convert to string. I think the first operation will round it off to 102, then when you divide you will get 1.02, two extra steps, but easier than manipulating strings.

Share on other sites
I thought this would help but it did not. I thought it would give me some other ideas too, but it did not.

Share on other sites
it might be best to store the DINT, after you multiply by 100, in the string. Then use string commands to put the decimal point where it should be.

Share on other sites
Works great if it is greater than or equal to 1.00. I'll keep working with it. I did the MUL then DTOS and then used INSERT to put in the decimal point. Probably can do a quick test to do a separate INSERT if the value is less than 1.00. Pretty close!

Share on other sites
Well, it sure is ugly but here is one working solution. Thanks for the ideas. Edited by BJR

Share on other sites
Simple solution: Create a string data type that has a length of four characters. Create a tag of that type. Use that tag as the destination in the RTOS.

Share on other sites
Wouldn't that still cause 1.01999998 to be 1.01 instead of 1.02?

Share on other sites
No. tried, tested & proven on L61 ver 16

Share on other sites
Interesting. Easy enough

Share on other sites
WOW. I like this a LOT better than the mess I created. Very nice. It does round like I needed.

Share on other sites
Nice. Edited by Alaric

Share on other sites
What an elegant solution. Makes me wonder why I even try. One of the reasons I like the 5000 series is the ability to create your own datatypes. And here I miss a prime opportunity to do so. tsk, tsk :)