Gawan
2 October 2020 07:08
1
Hi,
I have a global variable of datatype float.
I read this float in a function, do an explicit convert to float (just to be safe) and write my msg3.FloatValue with it:
msg3.Measure = 'Actual_Power_OUT';
msg3.StorageType = 'Keep';
msg3.FloatValue = parseFloat(global.get('AMIS.Strom_Einspeisung.Watt'));
In the next step I try to validate if the value (it is 0) is a float value.
if ((msg3.FloatValue || 99999.0) != 99999.0)
{
msg.head = "do something";
msg.body = "do something nice with " +msg.FloatValue;
}
But unfortunately it never steps into this IF. It seems that 0 compared to != 99999.0 gives false and I do ot understand why
Do you ?
BR
Gawi
E1cid
2 October 2020 07:52
2
if msg3.FloatValue
equals 0 or null
Then (msg3.FloatValue || 99999.0)
returns 99999.0
The problem is your use of the ||
operator. If msg3.FloatValue
has a value of 0
, then the expression in the if statement becomes:
if (( 0 || 99999.0) != 99999.0)
0
has a "false-like" value, which means the ||
operator will return the right-hand side - in this case 99999.0
. So the expression becomes: (99999.0 != 99999.0)
Gawan
2 October 2020 07:53
4
but why ?
shouldn't it compare 0 to 99999.0 and go into the IF clause ? (because of the IS NOT comparison)
is there a way to convert "0" into a valid float with value 0 ?
E1cid
2 October 2020 07:55
5
@knolleary explains it, 0 is a false like value, so the || returns the true value.
Try
if (msg3,FloatValue === 0 || (msg3.FloatValue || 99999.0) != 99999.0)
Assuming msg3.FloatValue
is ZERO...
if ((msg3.FloatValue || 99999.0) != 99999.0)
is the same as
if ((0 || 99999.0) != 99999.0)
is the same as
if (99999.0 != 99999.0)
Gawan
2 October 2020 08:59
7
is there a way to use 0 as a valid float so that I can compare with it ?
JavaScript doesn't really have the concept of floats and integers - they are all just numbers and can all be compared freely.
The parseInt
/parseFloat
functions are convenience functions for getting a number that may or may not have a decimal component.
In your code, you do:
msg3.FloatValue = parseFloat(global.get('AMIS.Strom_Einspeisung.Watt'));
That means msg3.FloatValue
will be a number type you can compare to any other number you want.
So the if statement you want can be:
if (msg3.FloatValue != 99999.0)
The problems you are hitting are due to your use of ||
- which you don't need here at all.
Gawan
2 October 2020 09:23
9
knolleary:
That means msg3.FloatValue
will be a number type you can compare to any other number you want.
So the if statement you want can be:
if (msg3.FloatValue != 99999.0)
The problems you are hitting are due to your use of ||
- which you don't need here at all.
The problem is, that quite often the msg3.FloatValue does not have a value, so its NULL oder NaN
For these cases I made the || workaround.
Is there another way to check if the variable has a valid value at all ?
system
Closed
1 December 2020 09:24
11
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.