Sign in to follow this  
Followers 0
alenck

Round Real to 2 decimal places

6 posts in this topic

How can i round real number to 2 decimal places in sysmac?

 

Share this post


Link to post
Share on other sites

Can you Multiply the real by 100 and place it in an integer and then divide that integer by 100 back into a real?

Share this post


Link to post
Share on other sites

i did this:

SCALE_REAL_TEMP:=SCALE_REAL_TEMP*100;
SCALE_REAL_TEMP_INT:=Round(SCALE_REAL_TEMP);
SCARE_REAL_TEMP_FINAL:=DINT_TO_REAL(SCALE_REAL_TEMP_INT);

Scale_Analogue_2dec :=SCARE_REAL_TEMP_FINAL/100;

But still i get more then 2 decimal places, ex. 10.0099999

Share this post


Link to post
Share on other sites

Real numbers have as many decimal places as they need.  I do not know of a way to truncate that to 2 digits.  You can typically do that on any operator interface terminal for display.

Share this post


Link to post
Share on other sites

Floating point doesn't support fractions like humans do.  If the fractional part's denominator isn't entirely divisible by two, you get a repeating "binarial" just like you get repeating decimals in base ten for denominators not divisible by ten.

Share this post


Link to post
Share on other sites

I created a function for this on my last project. It basically uses the method that @BobLfoot said, and also has an input so you can set how many decimal places you want. From memory, when looking at the output variable from this function, they still showed a heap of decimal places (ie. 123.45000000), but they were only ever trailing 0's. Personally, I needed it just to round off scaled temperatures & weights (which were on analogue inputs) to make my life a bit easier on the HMI side of things. I have attached the function below in case it might be of use.

Trunc Real Decimals Function.smc2

1 person likes this

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