Expand Value to 3 digits

Hi,

I have a function which delivers me a value from 0.0 to 999.9

e.g.: 8.3, 14.5, 342.9

I need to expand the value before the comma to 3 digits.

e.g. 008.3, 014.5, 342.9

How can I do this?

Br,
Johannes

If the value is already a String type and you know it will always have a decimal point and one digit after it, then you could do:

var value = "8.3";
value = value.padStart(5,"0");
// value is now "008.3"

But if the value is a Number type to begin with, you'll also have to deal with formatting the decimal point and the single digit after it. This is because a simple toString of the number 1.0 will give you "1" not "1.0". So for that, you can use the toFixed function:

var value = 1.0;
value = value.toFixed(1).padStart(5,"0");

I have tried the follwoing:

var value = msg;
value = value.padStart(5,"0");
return msg;

Getting errormessage:

TypeError: value.padStart is not a function

padStart is a function of String objects. In your code, you are assigning value to msg. That isn't what you want. I would guess the value you want is in msg.payload.

Have a read through this guide to working with messages in node-red
https://nodered.org/docs/user-guide/messages