Pass REAL to INT

vib

Member
Join Date
May 2022
Location
spain
Posts
7
Hello I have a question about SCL

if I have a real value
like
valor: INT

I would do
valor : = 1.5

Or this trhow me an error?

How can I save only the 1 of the 1.5 in a INT type?

Thanks,
Bless for this web, it's amazing.
 
You have not stated the platform (PLC you are using) you really need to use one of the in-built functions like REAL_TO_INT. it will depend on the PLC & it's functions there are some others like TRUNC but be careful, some conversion routines return 32 bit integers not 16.
 
Oh,
then you have to use this special function
ok
thanks
:)

You don't have to use a special function. it will depend on what plc you use and which language otions you have. The following code extract is taken from Siemens S7 STL language

Code:
mb02: L     #rTemp
      +R                                // Add offset to previous value
      L     1.000000e+004               // Check for max
      >R    
      JC    out
      TAK   
out:  RND   
      T     #iRewindSpeed10k            // Output the value
 
Read your post a little more, not sure what you are trying to do but you have an integer variable & trying to assign a real value is that correct, or is the real value from some other variable that is a real & you are trying to write it to an integer variable ?
If the value is fixed i.e. Valor:= 1.5 will not work but Valor:= 1 will.
Or if Valor is a real
for example:
Valor: releal;
My_Int: Integer;
Valor := 1.5;
REAL_TO_INT [Valor, My_Int];
But if as Manglemender suggests & it is Siemens then follow his suggestion.
Or RND[Valor, My_Int]; however, this may return a 32 bit long integer not a standard integer;
 
Reading the 1st post again, I think that this is the actual question
How can I save only the 1 of the 1.5 in a INT type?
If you want to convert a REAL to an INT and cutting off the fractional part, then use TRUNC.
edit:
To take a REAL, cut off the fractional part, and place in an INT you have to do
valor := DINT_TO_INT(TRUNC(1.5)) ;

If you want to convert a REAL to an INT using normal rounding, then use REAL_TO_INT. Notice, that will round 1.5 into 2.
 
Last edited:

Similar Topics

Hello, I am still new to PLC programming and I just got this job two year out of school so I don’t remember much. I was given a task were I have...
Replies
1
Views
171
So I'm having issues with a certain rung and one of my coworkers mentioned it may not allow the signal past the latch coil. For example in the...
Replies
27
Views
3,755
I’m a bit stuck on HMI (KTP-1200) programming… See the picture attached. The PASS or FAIL box should only appear when the toggle switch is...
Replies
7
Views
1,077
I've been given a line that has several sysmac c200Hx PLCs. The First station Im trying to wrap my head around has a NS8 HMI. I'm using Cx-one...
Replies
2
Views
751
See image below. Look at line 95. I tested all data type, but none of them works. https://ibb.co/H2Csr15 See this. TEMPDATA2 is used only...
Replies
3
Views
2,708
Back
Top Bottom