Convert number to Number of decimal places

TheWaterboy

Lifetime Supporting Member + Moderator
Join Date
May 2006
Location
-27.9679796,153.419016
Posts
1,924
I am writing a routine to convert and condition a raw value into a buffered value with a specified number of decimal places.
The formula I use for 2 decimal places is REAL=((TRN(value x 100) * .01)

Works well enough but I would like to make the multiplier terms operator configurable, i.e. i.e. 2 = 2 decimal places (2=100 and .01, 3=1000 and .001 etc).
How, without using a lookup table or case statement, can I turn a 2 into 100? I can invert that (1/x) to get the other term but the first one eludes me.
 
Finding its not perfect though, No doubt due to binary math, the value sometimes gets more decimals than I want. Is there no way to truncate or round to 2 decimal places in all cases.
 
Finding its not perfect though, No doubt due to binary math, the value sometimes gets more decimals than I want. Is there no way to truncate or round to 2 decimal places in all cases.

I'm not sure if this is related, but I know that some numbers can't be represented exactly by a Real, due to how things are handled in the background.
 
Well for those playing along at home I got what I wanted. But it certainly wasn't worth the effort except as an academic exercise. Check this monstrosity out (Hope it will open big enough to see) You will notice the rounding errors in the SUB function and a MOD function did the same thing.
The only part to come out with 2 places as desired is the REAL that gets generated by the strings.
Fun exercise. Going back to work now.

PLC code.jpg
 
Multiply the REAL by (10^number of places) and put the result into a DINT, then divide the DINT by (10^number of places) with the result going into a REAL.
 
Bob, how do you mask digits in the PLC?

Danny, Its not that easy, still leaves too many decimal places in the REAL because of binary math artifacts. I had forgotten about that when I started this quest.
 
Use and word - ANDW #00FF A352 D4000 - that will mask 2 digits. I typically use this function for the time clock for example - hours and minutes in the same word - the 00 blocks the minutes and the FF transfers the hours to a channel as above. This is in an Omron PLC - #FFF0 will block the last digit. Hope this helps.
 
There may be another instruction that you can use? I find ANDW really handy.
 
If so I can't find it. Can't just mask part of a DINT unless you turn it into a string, then all the options are available. No Worries, it was just an interesting quest.
 
Last edited:

Similar Topics

I'm working with SLC 500 Data File. I have a few numbers like 8.313452e+07 . I forget , how do you convert these numbers . Thanks so much.
Replies
11
Views
2,485
Firs of all I'm working with Structured Text on B&R Automation Studio 4.0: I have a binary message on a string (collected from serial port). The...
Replies
4
Views
8,157
Jees... Continuing from this thread... http://www.plctalk.net/qanda/showthread.php?t=85341 I now have the correct pressure in Torr and mBar...
Replies
24
Views
7,414
Number 0-4500, available from the on-site collection, how to convert 4-20 mA?To the analog output module PME6.0,IC694ALG392 Thank you
Replies
3
Views
2,145
I am working on a problem with a scanner. The data comes in as ASCII. I am scanning the quantity. The data looks like Q1^M or Q21^M (They can...
Replies
2
Views
3,071
Back
Top Bottom