Sign in to follow this  
Followers 0
Isa Ahmad

GXWorks 2 Rounding Float

4 posts in this topic

Trying to get rid of some decimal places. Right now we have a float that has 3 decimals and we need to reduce it to 2. We thought about trying to multiply by 100, converting to int to get rid of all the decimals after, then converting back to float, and then dividing by 100 to get only two decimals. But whenever we do the last floating point division the real value within the register has a decimal of .45999999999999999 instead of .46

 

Any suggestions on how to limit the amount of decimals either in the last division or another way to reduce the the amount of decimals originally?

Capture.png

Share this post


Link to post
Share on other sites

It isn't an error, just an artifact of binary fractions.  Similar to some fractions yielding repeating decimals (anything that has any factor other than 2 or 5 in the denominator).  In binary, if the fraction's denominator isn't a power of two, it will repeat.

Your HMI should round it for you.  You see the real thing in the PLC.

1 person likes this

Share this post


Link to post
Share on other sites

Yea the issue is we are passing it via BUFSND to and IoT application that does not do the rounding. We were hoping to round it somehow before sending it.

Share this post


Link to post
Share on other sites

Maybe it is an option to provide the data as a string to the IOT application? You can use the "DESTR" instruction were you can set the total number of digits (for example 6) and the required decimal places (2 in this case)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0