Number of decimals i function


How do i limit the number of decimals to 1 in this?

msg.payload = Number(msg.payload)
msg.payload = (((msg.payload)*10)-100)
return msg;

My output is like this now



This is the second thread today that this has been discussed (Problems using '.toFixed()'). It is generally best not to do this until you are about to display it, assuming that the reason you want to limit it is for display purposes. Don't throw away accuracy until the very last moment. Often the widget you are using for display has the ability to show numbers to a given number of decimals. See the other thread for how to use angular filters in dashboard nodes to do this.
If you really do need to truncate in a function then look at the .toFixed() function as used in that thread.



Do I sense a new load of college students starting their programming courses?!

1 Like


You could be right.



Which I hope as a community we welcome with patience, respect and gentle guidance, as a new group of people gain their first experiences of Node-RED, and possibly programming in general.



Certainly, well I do my best. One of the reasons I tend to give pointers to how to do things rather than provide a full solution is that I think users learn more from working out how to do something with guidance rather than being handed it on a plate.