Convert Floating Point to Integer in CLICK PLCs

There is no Float2Int instruction in the instruction set for CLICK PLCs from AutomationDirect*, but it is not difficult to convert floating point values to integers with that series. There are two methods I use: rounding with the MATH instruction and truncating with the COPY instruction. With either method, the key is to specify an integer memory location for the destination. This automatically casts the result as an integer.

Rounding with the MATH instruction

To round a value to the nearest whole number or integer when converting floating point values, use the MATH instruction to add 0.5 to the value. Then store the result in an integer register (the memory locations prefixed with DD or DS, see also CLICK PLC Data Types) to truncate the fractional portion.

For example, rounding 6.2 works like this: 6.2 + 0.5 = 6.7 which is then truncated to 6 when stored as an integer:

Rounding the floating point value in a memory location and converting it to and integer
Converting the floating point value in memory location DF99 by storing it in integer location DS3

And 6.5 + 0.5 = 7.0, which truncates to 7:

Rounding the floating point value in a memory location and converting it to and integer
Again converting the floating point value in memory location DF99 and storing it in integer location DS3, this time the value gets rounded up

The comparison instructions (DF99 > 0) at the beginning of the above rungs are only for demonstration purposes, to display the value of DF99.

Truncate using the COPY instruction

If you need to remove the fractional value of a floating point value without rounding, COPY the floating point value to an integer register and, if necessary, copy it back again. For the example below, a value of 6.8 is copied into floating point memory location DF99 in the first COPY instruction. In reality, this value might have come from a calculation. The second COPY instruction truncates the fractional 0.8 portion of the original value. It does this because an integer memory location, DS3, is specified as the Destination. So the fractional portion has nowhere to go. If the truncated value needs to be in the original floating point memory location, copy the value back from the integer memory location to the floating point memory location.

Truncating a floating point value by copying it to an integer memory location
Truncating a floating point value

Note that the screenshot of the truncation example is a little misleading because it shows the values in the memory locations after the rung has been processed by the PLC. So the first COPY instruction shows DF99 containing 6 already, even though DF99 does not contain this value until the third COPY instruction.

If you are using a Do-More PLC, see the ROUND() function of the MATH instruction.

The Do-More MATH function rounding value of R99 from 6.500 to 7.000 and storing the result in R100
The Do-More MATH function rounding value of R99 from 6.500 to 7.000 and storing the result in R100

* This site is not affiliated with AutomationDirect.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *