# Padding zeros in a function

**URL:** <https://discourse.nodered.org/t/padding-zeros-in-a-function/4302>\
**Category:** General\
**Created:** [26 October 2018 00:16 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302 "2018-10-26T00:16:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beothuk](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Beothuk](https://discourse.nodered.org/u/Beothuk)\
**Post date:** [26 October 2018 00:16 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/1 "2018-10-26T00:16:25Z")

</div>

I am using a slider to generate numbers from 0 to 150. I need all numbers to be generated with preceeding zeros if they are less than 3 digits. So 10 would be 010, 8 would be 008,100 remains unchanged. I am sure there is a way to accomplish this in a function but I cannot find a suitable example. Can someone give me a hand?

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [26 October 2018 00:47 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/2 "2018-10-26T00:47:01Z")

</div>

Can be achieved with the new ES8 feature / function named "padstart" in a function node:

> **[String.prototype.padStart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)**
>
> The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.

```auto
msg.payload = "8".padStart(3, "0");
return msg;

```

Results:

"008"

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2018 08:28 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/3 "2018-10-26T08:28:02Z")

</div>

> [@Beothuk](#):
>
> I need all numbers to be generated with preceeding zeros if they are less than 3 digits

I am a bit of a pedant (which is a good thing for those developing with computers) so I have to point out that to ask for a Number in javascript to have leading zeros does not make sense. A number is a number. I think probably what you are asking is how to convert a number to a string representing the number and with leading zeros. Thus the number 10 would be converted to the string "010". If that is the case then @Andrei's result may well be the right way to go. Note though that generally you should do this at the point you display it, keep it as a number in the rest of the flow. If you are using a dashboard node to display the value you can do the formatting in the node itself by using

```auto
{{("000"+msg.payload.toString()).slice(-3)}}

```

in the Value Format field. For example:

```auto
[{"id":"db73c0d3.6c81f8","type":"ui_text","z":"6dc690a3.1abc88","group":"90837d20.135ef","order":0,"width":0,"height":0,"name":"","label":"padded","format":"{{(\"000\"+msg.payload.toString()).slice(-3)}}","layout":"row-spread","x":361.5,"y":399,"wires":[]},{"id":"4b0c11c8.19b34","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"27","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":144.5,"y":397,"wires":[["db73c0d3.6c81f8"]]},{"id":"4b8f17ff.727fb8","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":146,"y":469,"wires":[["db73c0d3.6c81f8"]]},{"id":"90837d20.135ef","type":"ui_group","z":"","name":"Default","tab":"bbb172bf.44157","disp":false,"width":"6","collapse":false},{"id":"bbb172bf.44157","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2018 08:30 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/4 "2018-10-26T08:30:25Z")

</div>

> [@Colin](#):
>
> {{("000"+msg.payload.toString()).slice(-3)}}

Or, of course, even better

```auto
{{msg.payload.toString().padStart(3,"0")}}

```

---

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [9 January 2021 20:54 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/5 "2021-01-09T20:54:08Z")

</div>

Bumping an old thread as I have not been able to get Colin's `{{msg.payload.toString().padStart(3,"0")}}` from post 4 to work, likely operator error on my part due to inexperience..

I have a function converting CPU temp to F° and rounding it to two places. Trouble occurs when the number shifts from xx.xx to xxx.xx and back, then the decimal place is off in my file-write. There may be an easier way to align the decimal, but I stumbled on this and am puzzled that changing either the start or pad character makes no difference to the returned msg.

My code:

```auto
//msg.payload = Math.round ((msg.payload / 1000.0 * 9/5 + 32)*100) /100;
//msg.payload = `CPU Temp:\t${msg.payload}\t${msg.myymd} @ ${msg.mytimes}\r`

msg.payload = 97.43
  {{msg.payload.toString().padStart(1,"0")}}
return msg;

```

I commented out the two working lines and have been using a fixed 2 or 3 digit number for payload. Changing the first number adds/subtracts nothing. Logically, the start should be 6 but running it from 0 to 50 results in neither padding or truncating. Result is always the msg.payload fixed value from the line above. Changing the 0 to something other than a number gives an error. Ideally it would be a space.

Russ

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 January 2021 21:14 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/6 "2021-01-09T21:14:43Z")

</div>

In post 3 (of which post 4 was a followup) I said

> [@Colin](#):
>
> If you are using a dashboard node to display the value you can do the formatting in the node itself by using

That syntax is the one to use inside a dashboard display node to format the data at the point of display, which is usually the best way to do it. If you want to use it in javascript then it should be something like  
`msg.payload = msg.payload.toString().padStart(1,"0")`  
However, if you are feeding it to a display node then do it there.

---

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [10 January 2021 17:57 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/7 "2021-01-10T17:57:58Z")

</div>

Thanks for the clarification. No display node, straight to a log file. My new code:

```auto
var aVar, bVar

aVar = Math.round ((msg.payload / 1000.0 * 9/5 + 32)*100) /100;
bVar = aVar.toString().padStart(6," ");
msg.payload = `CPU Temp:\t${bVar}\t${msg.myymd} @ ${msg.mytimes}\r`

return msg;

```

Decided to try variables in case I was somehow wrecking the payload. I know it's not random, but somewhere there's a gremlin that pushes 3-digit temps one space off as in line 4 and line 8 from my file: I'll have to build a temporary function node and feed it incremental numbers -\> debug and see what's up. Other than that, it works great.

```auto
CPU Temp:	101.53	2021-01-09 @ 15:24:54
CPU Temp:	101.53	2021-01-09 @ 15:25:33
CPU Temp: 99.59	2021-01-09 @ 15:27:39
CPU Temp: 120.9	2021-01-09 @ 15:29:20
CPU Temp:	126.71	2021-01-09 @ 15:29:36
CPU Temp:	117.99	2021-01-09 @ 15:29:54
CPU Temp:	112.18	2021-01-09 @ 15:30:06
CPU Temp: 105.4	2021-01-09 @ 15:30:35
CPU Temp:	101.53	2021-01-09 @ 15:32:47
CPU Temp:	100.56	2021-01-09 @ 15:33:27
CPU Temp:	100.56	2021-01-09 @ 15:33:49
CPU Temp:	100.56	2021-01-09 @ 15:34:07
CPU Temp: 99.59	2021-01-09 @ 15:35:16
CPU Temp: 97.66	2021-01-09 @ 15:58:59
CPU Temp: 97.66	2021-01-09 @ 16:58:59

```

Thanks!

Russ

---

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [10 January 2021 20:03 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/8 "2021-01-10T20:03:34Z")

</div>

Well, apparently when there are actual trailing zeroes in the Math.round function, they are removed. How would that be fixed? Do I need a padEnd function?

Russ

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 January 2021 20:27 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/9 "2021-01-10T20:27:00Z")

</div>

Use `toFixed()` instead of `round()`

---

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [10 January 2021 20:49 UTC](https://discourse.nodered.org/t/padding-zeros-in-a-function/4302/10 "2021-01-10T20:49:24Z")

</div>

Perfecto, all lined up nice and pretty!

Thanks for your help,

Russ
