Rounding off value after division

Cundalini

Member
Join Date
Oct 2005
Location
Frankston Soth
Posts
6
Hi all,Is anyone able to help me with a method for rounding off a value following a division instruction. I am using Siemens S7 and plan to display the resulting value on an OP7 panel. Cheers Cundlaini.
 
Here's one way

If it's a Decimal value you could move it into a INT or DINT or some other data then move it back.... Would need some more info on what type of number you are trying to round and to what?

IE: 1.5 = 2, 44 = 50, 8 = 10, etc...

I've never used the Siemans before but here is another way if it's an INT based number (no decimal) if you have the command available.

You could Divide the Number using a Modulo Style Command from the AB World where the following happens

111 / 10 = 1

The Ab command gives you the decimal portion of the Division.. then you would compare it to being Greater then or equal to 5 and round up etc...
 
Thanks for the reply wnkook.

Here's what I am trying to do.
I have a decimal value in a DB Word, say DBW1 and a decimal value in DBW5. I would like make a calculation to display DBW5 as a percentage of DBW1 and load this into another word, so I can display it on an OP7 user panel.
It is better for the user if the display is an integer, so I would like to round down after the calculation.

I am not very familiar with AB, and as far as I know S7 has nothing like the "Modulo Style command" you mentioned.

Thanks, Cundalini
 
You have Step 7 software, right? If so, there is everything you need to know in the help files. Just do a search on "remainder" and "divide".

When you divide two integers, the result is already rounded. If you only want the remainder, then there is the MOD instruction, but I think you just need a rounded value.
 
Cundalini,

To Clarify you would have a result of 4.567% and you want to round this down to 4%.

Now normally if we move the above number directly to an Integer the item is rounded up.

ok so here is what we could try,

Now if we move the result into an integer word, using the above number we could get 5

Now if we subtract 4.567 by the integer result of 5 we will either get 0 if the PLC performs a rounding operation before the subtraction or we will get -0.433

We want the result word of the subtract to be a decimal word...

Now we can perform a comparision, if the number is less then 0 then subtract 1 from the result of the division. If it is zero leave it as is zero or above we can leave the result as be...


The above will only work if the PLC converts the 5 we are subtracting into a decimal before performing the subtraction... if it converts the decimal into an integer this will not work... But by us calling our result as a decimal it should convert the Integer to decimal and the above logic would work..

Now you'd just have to add this into a function call or Block.
 
Last edited:
Late entry, I know, but just to add a few extra options.

You should find that S7 has standard instructions ROUND, CEIL, FLOOR and TRUNC for 'trimming' REAL numbers to DINTs -

CEIL always rounds up to the next whole number (so 3.1 becomes 4, and 3.9 becomes 4);
FLOOR always rounds down to the previous whole number (so 3.1 becomes 3, and 3.9 becomes 3);
TRUNC simply throws away the portion following the decimal point (so 3.1 becomes 3, and 3.9 becomes 3)
ROUND rounds to the nearer whole number (so 3.1 becomes 3, and 3.9 becomes 4. If there are two whole numbers equally near then ROUND returns the even one. So 3.5 becomes 4).

Regards

Ken
 
Originally posted by Ken M:

ROUND rounds to the nearer whole number (so 3.1 becomes 3, and 3.9 becomes 4. If there are two whole numbers equally near then ROUND returns the even one. So 3.5 becomes 4).

It is defined and repeatable. I'm just not so sure I like that method. It would seem to be more freindly if ROUND would always go one direction if equidistant, either up or down.

Keith
 
An easier way.

jacekd said:
I'm afraid that mathematics does not care what you like or not.

It isn't a question of math. It is convention. Thre are four rounding methods in the IEEE floating point standard and the round toward zero has been left out.

However, I wouldn't care. I would do this

( DBW5 * 100 + DBW1 / 2 ) / DBW1

This avoids the need for floating point math. This is how I do it when no floating point is available. There is no need to use floating point in this example because the number can't overflow 32 bit DINTs.

Keith, if you always round up when the fraction is .5 then you will add a bias to the calculations that can be a killer in some applications. IEEE did it right.

Notice the rounding is added BEFORE the divide.
 
Last edited:

Similar Topics

I have a Beckhoff PLC with many AI modules. Most devices are loop powered using the same +24 VDC power as the Beckhoff Bus Coupler and all is...
Replies
5
Views
670
So far so good, Almost done with this migration stuff Someone please help me on this one: ERROR IS: ''Bit and LSBit triggered alarms that used...
Replies
2
Views
1,704
Dear All, I need to round off the analog value FOR EXAMPLE: 123.45 to 123 Please prove solution. Thanks
Replies
14
Views
20,879
Hi all, I'm connecting several 4-20mA sensors together in parallel (only one shown below) The enclosure is ABS plastic with metal backplate DAQ...
Replies
5
Views
274
I have Rhino 120 to 24 dc power supply and it has 2 Positive and 2 negative terminals on the DC side but no ground terminal. Should I connect one...
Replies
9
Views
436
Back
Top Bottom