# Format decimal place in a function

**URL:** https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449
**Category:** General
**Created:** [12 September 2019 05:02 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449 "2019-09-12T05:02:10Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 05:02 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/1 "2019-09-12T05:02:10Z")

</div>

I have a function with the following..

flow.set('Paywave\_',msg.payload.Paywave\_);  
msg.payload = msg.payload.Paywave\_$;  
return msg;

Paywave\_$ appears with a lot of decimal places that I need to reduce to one. However as this value is being sent to a csv file and not to a dashboard display I cannot use the Value format.  
How can I change this in the function. I have tried .tofixed(1) but this creates problems and |number:1 does not seem to work.  
Thanks in advance

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [12 September 2019 05:18 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/2 "2019-09-12T05:18:38Z")

</div>

```auto
var originalNumber = 5.78945612301
var formatedString = originalNumber.toFixed(1) // 5.7 (string)
var formatedNumber = parseFloat(formatedString) // 5.7 (number)
			
// can be combined
var formatedNumber = parseFloat(originalNumber.toFixed(1))
			
// or if original is not needed anymore apply it directly to original
originalNumber = parseFloat(originalNumber.toFixed(1))

```

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 08:08 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/3 "2019-09-12T08:08:59Z")

</div>

Yes tried that.. but got this error..  
"TypeError: Paywave\_$.tofixed 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: [12 September 2019 08:18 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/4 "2019-09-12T08:18:47Z")

</div>

@tfb what exactly does `msg.payload.Paywave_$` contain?

If you pass it to the Debug sidebar, you can check if it is a Number type or a String type. What you do next will depend on that.

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 09:03 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/5 "2019-09-12T09:03:04Z")

</div>

Hi this is my first application so am doing a bit of learning here...so thanks for the help..  
below is what I see in the debug - I believe it is a number

9/12/2019, 8:52:50 PM[node: 8b79517b.291ef](https://192.168.0.100:1880/#)msg : Object

{ payload: 2.299999952316284, topic: "", \_msgid: "6d24dd2d.aef9d4" }

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [12 September 2019 09:06 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/6 "2019-09-12T09:06:41Z")

</div>

That shows that `msg.payload` is a number.

But as `msg.payload` is a number there is no `msg.payload.Paywave_$`

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 09:27 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/7 "2019-09-12T09:27:44Z")

</div>

Hi ukmoose,  
Paywave\_ is a variable coming from a device that has several other variables. Paywave\_ is producing the payload of 2.299999952316284 in the first instance.  
I need to reduce this down to 2.3

cheers

---

<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: [12 September 2019 09:33 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/8 "2019-09-12T09:33:56Z")

</div>

> [@tfb](#):
>
> Paywave\_ is a variable coming from a device

Have you added the Debug node _after_ the Function node you mention at the start of your first post? In which case you are showing us what comes _out_ of the Function node, not what is passed _in_ to the Function node.

That would explain why the Debug output doesn't show anything related to `Paywave_`.

It would help if you could add the Debug node alongside the Function node so we see exactly what message you are starting with. For example, in your Function node you reference `msg.payload.Paywave_` in the first line, but `msg.payload.Paywave_$` in the second line.

> [@tfb](#):
>
> "TypeError: Paywave\_$.tofixed is not a function"

What code exactly did you try?

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 10:10 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/9 "2019-09-12T10:10:20Z")

</div>

The reason msg.payload.Paywave\_ appears with and without the is that when I type it directly into this message window the $ (dollar sign) disappears. Both cases have the dollar sign.

I have addded the debug node along side the function node but it does not show anything related to Paywave\_$ (dollar sign) .  
9/12/2019, 9:56:17 PM[node: 8b79517b.291ef](https://192.168.0.100:1880/#)msg : Object

{ payload: object, topic: "", \_msgid: "2107797f.92e1f6" }

I guess this is because it is coming from a S7 PLC node which can have multiple variables.  
Essentially Paywave\_ (dollar sign) is a float register coming from the PLC. When I change the vlue in the PLC the value in Paywave\_ changes accordingly except that it has a multiple decimal places.

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [12 September 2019 10:14 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/10 "2019-09-12T10:14:20Z")

</div>

> [@tfb](#):
>
> { payload: object, topic: "", \_msgid: "2107797f.92e1f6" }

That shows that it is an object. There should be a triangle by it to expand it to see the objects contents as in this image from teh page about working with messages in the docs  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7754619cf8cf0a69bc4c57417704a0b234548a2a.png) [https://nodered.org/docs/user-guide/images/messages\_debug.png](https://nodered.org/docs/user-guide/images/messages_debug.png)

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 10:21 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/11 "2019-09-12T10:21:26Z")

</div>

Thanks you are correct...

9/12/2019, 10:20:27 PM[node: 8b79517b.291ef](https://192.168.0.100:1880/#)msg.payload : Object  
object  
Paywave\_$: 2.299999952316284

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [12 September 2019 10:34 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/12 "2019-09-12T10:34:13Z")

</div>

So following what @hotNipi said  
`msg.payload.Paywave_$ = parseFloat(msg.payload.Paywave_$.toFixed(1))`

---

<div class="post-metadata">

### Author: ![tfb](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@tfb](https://discourse.nodered.org/u/tfb)
#### Post date: [12 September 2019 10:55 UTC](https://discourse.nodered.org/t/format-decimal-place-in-a-function/15449/13 "2019-09-12T10:55:48Z")

</div>

Ok so I put that whole line in and it works - I must have entered it into the wrong location earlier.

Thank you very much to hotNipi, knolleary and ukmoose
