Converting float to a formatted string

Hi there. Anybody know the best way to convert floats with an unknown number of digits before as well as after the dot, to a string with specific length so that the point is always at the same position in the string?
This is necessairy for placing in columns.

May be JSONata formatNumber() may help

[{"id":"efe8c79e.7b253","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12.23456","payloadType":"num","x":230,"y":1100,"wires":[["b52932b3.d6fa68"]]},{"id":"b52932b3.d6fa68","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$formatNumber($$.payload, '0000000.0000000')\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1100,"wires":[["38c93b0.6dfd3c6"]]},{"id":"a91958b1.07a2c8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12.23","payloadType":"num","x":210,"y":1060,"wires":[["b52932b3.d6fa68"]]},{"id":"38c93b0.6dfd3c6","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1100,"wires":[]}]
$formatNumber($$.payload, '0000000.0000000')	

input 12.3456 output 0000012.3456000

If you are displaying the value on dashboard, you can use angular formatting in many of the components. E.g. {{ payload | number: '1.2-2' }}

@Steve-Mcl , thanks for the suggestion, but it is ment for a table.

@E1cid , That is a very good solution!! I only need to loose the leading zero's.

@E1cid , I don't seem to be able to use this construction in a function node. :thinking:

I need this for a lot of calculated variables.

Change the leading "0" to "#" e.g. "#######.0000000"

It is JSONata not javascript
javascript equivalent is

msg.payload = msg.payload.toFixed(7);
return msg;
2 Likes

@E1cid , now I feel stupid. :grin: Always thought "toFixed()" was for truncating.....pff.

Thanks !!

Hello,

Try this expression :

return msg.payload = msg.payload.toFixed(6);

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