Announcement

Collapse
No announcement yet.

Rounding a floating point value.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rounding a floating point value.

    I would like to be able to round a floating point value to the nearest whole integer. Example:

    9.4 would be 9.
    9.6 would be 10.

    The integer divide in the math task always seems to round down. As does simply copying the float tag to an integer tag.

    This simple operation eludes me. Any ideas on how I might accomplish this?

  • #2
    Add .5 before doing the integer divide.
    thePLCguy

    Bernie

    Comment


    • #3
      Wow. I feel a little dumb that I didn't think of that.

      Thanks!

      Comment


      • #4
        If I use a float to integer in the CPD instruction it rounds up if 9.5 or above and down if 9.4 or below. So if I have a float tag source and a integer tag as the destination ( both 32 bit) and I put 9.0 to 9.4 in the float I get 9 in the integer. If I put 9.5 to 9.9 I get 10 in the integer. I do not have to do any math instruction.

        Comment

        Working...
        X