# Expand Value to 3 digits

**URL:** https://discourse.nodered.org/t/expand-value-to-3-digits/21690
**Category:** General
**Created:** [14 February 2020 10:51 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690 "2020-02-14T10:51:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ebnerjoh](https://avatars.discourse-cdn.com/v4/letter/e/ebca7d/32.png) [@ebnerjoh](https://discourse.nodered.org/u/ebnerjoh)
#### Post date: [14 February 2020 10:51 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/1 "2020-02-14T10:51:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 February 2020 10:58 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/2 "2020-02-14T10:58:24Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

### Author: ![ebnerjoh](https://avatars.discourse-cdn.com/v4/letter/e/ebca7d/32.png) [@ebnerjoh](https://discourse.nodered.org/u/ebnerjoh)
#### Post date: [14 February 2020 11:16 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/3 "2020-02-14T11:16:56Z")

</div>

I have tried the follwoing:

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

```

Getting errormessage:

```auto
TypeError: value.padStart is not a function

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 February 2020 11:26 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/4 "2020-02-14T11:26:13Z")

</div>

`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`.

---

<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: [14 February 2020 11:36 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/5 "2020-02-14T11:36:55Z")

</div>

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

---

<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: [28 February 2020 11:36 UTC](https://discourse.nodered.org/t/expand-value-to-3-digits/21690/6 "2020-02-28T11:36:55Z")

</div>

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