Padding zeros in a function

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?

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

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

Results:

"008"

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

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

in the Value Format field. For example:

[{"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"}]
1 Like

Or, of course, even better

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

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:

//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

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

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.

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

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.

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

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

Use toFixed() instead of round()

Perfecto, all lined up nice and pretty!

Thanks for your help,

Russ