BASIC convert FLOAT to INT


#1

Hi team!
i need to copy the integer part of a S73&400 float tag to a MEM integer tag:
my S73&400 float tag is PlcStatusTag
my MEM int tag is IntStatusTag

if PlcStatusTag=1.0 the IntStatusTag must be setted to 1
if PlcStatusTag=1.2 the IntStatusTag must be setted to 1
if PlcStatusTag=23.4 the IntStatusTag must be setted to 23

is there any DIV or TRUNC or INT funcion ?


#5

When you truncate it, do you always want it to round down?

Or if it’s something like 1.9 do you want it to round up to 2?


#6

i have both case


#7

Here’s some BASIC IDE code I wrote to be able to do this

  • Test is the real floating point value that is being read from the PLC
  • Test2 is a tag that is set to be an Integer type
  • b is just a variable that is used to take the difference between the real and integer value since by default if you read in a real value as an integer it will just cut off the decimal and do no rounding.

For example: if test@ = 1.7, then test2@ will equal 1 and will cause b to equal 0.7

OnChange “test”, “@OutTest()”
b = 0
Function OutTest()
Test2@ = test@
b = Test@ - Test2@
if b >= 0.5 then
test2@ = test2@ + 1
else
test2@ = test@
endif
Print test2@
EndFn


#8

thank you for the code!

now the problem is that my tag name contains () , for example PlcStatusTag(0) or IntStatusTag(0), so i cant use IntStatusTag(0)@=PlcStatusTag(0)@

i must use SETIO “IntStatusTag(0)”,“PlcStatusTag(0)”

i have no debugging errors but the SETIO seem that doesn’t convert from float to int


#9

Hello,

Would it be possible to change the name of your tag to IntStatusTag0 without the parentheses?


#10

unfortunately no is not possible…


#11

#12

unfortunately no, i must use the ( )


#13

Hello,

Can you try and do something like the following?

b = getio “PlcStatusTag(0)”


#14

Tim,
the problem is to assign the value of a float tag into a int tag ;
if i use SETIO “IntStatusTag(0)”,“PlcStatusTag(0)” , the IntStatusTag(0) is set forever to a wrong value, if you change the value of PlcStatusTag(0), the IntStatusTag(0) value remain at the first value

you can try to reproduce it using two mem tag:

in basic i use this script
prog1

prog2

if you set manually the value of the float tag (in this case 5.7), the output tag is setted to 1082101684 and then remain setted to this value.

no errors in console or in Log

others way to set the IntStatusTag(0) generates debug errors


#15

I was getting that error when I was trying to use a second value with the parentheses, but I was able to get around that by using a different internal tag with an int type.

Try this

OnChange “test”, “@OutTest()”
b = 0
Function OutTest()
Test2@ = test@
b = Test@ - Test2@
If b >= 0.5 Then
test2@ = test2@ + 1
Setio “IntStatusTag(0)”,test2@
Else
test2@ = test@
Setio “IntStatusTag(0)”,test2@
Endif
Print test2@
EndFn