# parseInt() sorry, but I seem to be missing something

**URL:** <https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507>\
**Category:** General\
**Created:** [12 April 2023 21:24 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507 "2023-04-12T21:24:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 April 2023 21:24 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/1 "2023-04-12T21:24:56Z")

</div>

With all the recent stuff I've been doing, asking and told I would have thought I would have got this down pat by now.

Seems not.

Code extract:

```auto
let d = msg.delay;
d = parseInt(d / 10000);
if (d > 10)
{
    d = d / 60
}

node.status({text:msg.payload + " " + d});
return msg;

```

Of interest: `d`.

So is `d` an integer or not?

Why?  
This is what I see below the node:

 ![Screenshot from 2023-04-13 07-23-17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/087758db2cb41fc865621c5ff2c5327ff8be8db8.png)

`4.4333333333333333333333` doesn't qualify to me as an integer. 😕 😉

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 April 2023 21:43 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/2 "2023-04-12T21:43:36Z")

</div>

Maybe msg.delay was 2600000 to start with. So you get to the if with d == 260. 260/60 is 4.333333  
parseint is a function. Not a type. You have to call it whenever you need it.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2023 05:49 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/3 "2023-04-13T05:49:38Z")

</div>

Ok, I guess the _final_ `/60` could be throwing a spanner in the works.

Sorry.  
And thanks.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [13 April 2023 09:27 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/4 "2023-04-13T09:27:03Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> let d = msg.delay;
> d = parseInt(d / 10000);
> if (d > 10)
> {
> d = d / 60
> }
> 
> node.status({text:msg.payload + " " + d});
> return msg;
> 
> ```

Adding some background here:

The [docu](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) says: The **`parseInt()`** function parses a string argument and returns an integer.

There's an additional note:

**Note:** JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. `parseInt()` and [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) only differ in their parsing behavior, but not necessarily their return values. For example, `parseInt("42")` and `parseFloat("42")` would return the same value: a [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) 42.

Thus you're a lucky person that JavaScript is really forgiving & `parseInt(d / 10000)` works at all. Behind the scenes, it's doing several (!) type conversions to process your request:

Pseudo code:

```auto
d => cast to number
calc d/10000 => result, typeof number
result => cast to string
perform parseInt(result) => d, typeof number

```

`d` may look like an integer, yet as Javascript has no `integer` type, it's still of type `number`.  
Thus `d/60` is a division of type `number` ... creating a result of type `number`.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2023 09:29 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/5 "2023-04-13T09:29:21Z")

</div>

Thanks.

But as pointed out to me by @dceejay the latter `/60` was throwing things into getting that weird number I showed on the screen shot.

I have since changed the code to:

```auto
d = d / 1000;
if (d > 10)
{
    d = parseInt(d / 60);
}

```

Works fine now. 🙂

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [13 April 2023 09:49 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/6 "2023-04-13T09:49:27Z")

</div>

> [@Trying\_to\_learn](#):
>
> Works fine now. 🙂

Sure it does. 😉  
Being a bit picky here, it's yet quite 'wrong'.  
`d` is not a `string`, it's already of type `number`.  
To round to an integer-like value, `Math.round()` or `Math.floor()` are your friends here...

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 April 2023 09:51 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/7 "2023-04-13T09:51:44Z")

</div>

Ok. No problems.

The _problem_ - this end - is the lack of my knowledge of all the commands.

I'll go off and see if I can get my head around those commands.

---

<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:** [27 April 2023 09:51 UTC](https://discourse.nodered.org/t/parseint-sorry-but-i-seem-to-be-missing-something/77507/8 "2023-04-27T09:51:48Z")

</div>

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