# Coverting and validating FLOAT does not work

**URL:** <https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656>\
**Category:** General\
**Created:** [2 October 2020 07:08 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656 "2020-10-02T07:08:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [2 October 2020 07:08 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/1 "2020-10-02T07:08:04Z")

</div>

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:

```auto
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.

```auto
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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 October 2020 07:52 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/2 "2020-10-02T07:52:09Z")

</div>

if `msg3.FloatValue` equals 0 or null  
Then ` (msg3.FloatValue || 99999.0)` returns 99999.0

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 October 2020 07:52 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/3 "2020-10-02T07:52:50Z")

</div>

> [@Gawan](#):
>
> In the next step I try to validate if the value (it is 0) is a float value.
> 
> ```auto
> if ((msg3.FloatValue || 99999.0) != 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:

```auto
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)`

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [2 October 2020 07:53 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/4 "2020-10-02T07:53:53Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 October 2020 07:55 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/5 "2020-10-02T07:55:20Z")

</div>

@knolleary explains it, 0 is a false like value, so the || returns the true value.  
Try

```auto
if (msg3,FloatValue === 0 || (msg3.FloatValue || 99999.0) != 99999.0)

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 October 2020 07:58 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/6 "2020-10-02T07:58:58Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [2 October 2020 08:59 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/7 "2020-10-02T08:59:16Z")

</div>

is there a way to use 0 as a valid float so that I can compare with it ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 October 2020 09:05 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/8 "2020-10-02T09:05:22Z")

</div>

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:

```auto
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:

```auto
if (msg3.FloatValue != 99999.0)

```

The problems you are hitting are due to your use of `||` - which you don't need here at all.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [2 October 2020 09:23 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/9 "2020-10-02T09:23:02Z")

</div>

> [@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:
> 
> ```auto
> 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 ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 October 2020 09:24 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/10 "2020-10-02T09:24:39Z")

</div>

You can use the `isNaN` function - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 December 2020 09:24 UTC](https://discourse.nodered.org/t/coverting-and-validating-float-does-not-work/33656/11 "2020-12-01T09:24:43Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
