# Split up numbers

**URL:** <https://discourse.nodered.org/t/split-up-numbers/14987>\
**Category:** General\
**Created:** [1 September 2019 01:39 UTC](https://discourse.nodered.org/t/split-up-numbers/14987 "2019-09-01T01:39:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LazzaWA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lazzawa/32/10616_2.png) [@LazzaWA](https://discourse.nodered.org/u/LazzaWA)\
**Post date:** [1 September 2019 01:39 UTC](https://discourse.nodered.org/t/split-up-numbers/14987/1 "2019-09-01T01:39:58Z")

</div>

hi all , i'm looking at getting my snips AI to change my tv channels for me by using a RMpro.

I can get snips to send me the correct intent and in node red i can change that message to the parts i need IE:

msg.payload.data.entity\_id = channel number  
msg payload = 54 \<---- _thats the channel number_

how do i now change that payload to a single 5 and a single 4 so i can then tell the RMPRO to push button 5 and button 4 on my remote ? im picking id also have to work out how to write a function so if number is double digits split the number up but if its a single number like 7 then add a 0 in front of it like 07

cheers.

Edited : should also mention im fairly new to node red and formula's etc so pointing me in the right direction to research and learn is also appreciated. however examples are awesome 🙂

---

<div class="post-metadata">

**Author:** ![guysqr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guysqr/32/11551_2.png) [@guysqr](https://discourse.nodered.org/u/guysqr)\
**Post date:** [1 September 2019 03:35 UTC](https://discourse.nodered.org/t/split-up-numbers/14987/2 "2019-09-01T03:35:52Z")

</div>

You can use split with the empty string argument to split characters:

```auto
let numParts = msg.payload.split('');
if (numParts.length === 1) {
  numParts.unshift('0');
}
console.log(numParts);

```

You then test the length of the array and unshift the '0' onto the array if it's only one character.

Note these numbers are still actually strings, so you may need to recast them at some point if you need them to act as numbers later in your code.

---

<div class="post-metadata">

**Author:** ![LazzaWA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lazzawa/32/10616_2.png) [@LazzaWA](https://discourse.nodered.org/u/LazzaWA)\
**Post date:** [1 September 2019 04:06 UTC](https://discourse.nodered.org/t/split-up-numbers/14987/3 "2019-09-01T04:06:32Z")

</div>

thanks for that. think i may of actually worked out another way by trial and error as well. (albeit right or wrong it seems to work)

```auto
if (msg.topic == "channel number") {
    if (msg.payload !== null) {
        var str = msg.payload;
        var num1 = str.slice(0, 1);
        var num2 = str.slice(1, 2);
        msg.num1 = num1;
        msg.num2 = num2;
        return msg;
    }
}

```

your method looks better than mine though so ill test that out too 🙂

---

<div class="post-metadata">

**Author:** ![guysqr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guysqr/32/11551_2.png) [@guysqr](https://discourse.nodered.org/u/guysqr)\
**Post date:** [1 September 2019 08:10 UTC](https://discourse.nodered.org/t/split-up-numbers/14987/4 "2019-09-01T08:10:54Z")

</div>

Your solution won't zero-pad the result if you only get a single digit channel value.
