Format decimal place in a function

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

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))
1 Like

Yes tried that.. but got this error..
"TypeError: Paywave_$.tofixed is not a function"

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

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 PMnode: 8b79517b.291efmsg : Object

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

That shows that msg.payload is a number.

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

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

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.

What code exactly did you try?

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 PMnode: 8b79517b.291efmsg : 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.

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/images/messages_debug.png

Thanks you are correct...

9/12/2019, 10:20:27 PMnode: 8b79517b.291efmsg.payload : Object
object
Paywave_$: 2.299999952316284

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

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