Convert EU to USA number formatting

We had a quick fun question come in the other day…

What is a simple Javascript code to convert European to North American number format:

North American example: 14,074.000
European example: 14.074,000

@torchard was hot on the problem solving trail and came up with 3.5 answers.
Change node, Javascript, JSONata and regex.

Here are the three solutions in one flow:

[{"id":"6f5ff02d.034d9","type":"change","z":"4118d274.45b1fc","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":".","fromt":"str","to":"#","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":",","fromt":"str","to":".","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"#","fromt":"str","to":",","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1780,"y":300,"wires":[["3fcbd991.7cfec6"]]},{"id":"cccb57ae.18de18","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14,074.000","payloadType":"str","x":1580,"y":260,"wires":[["6f5ff02d.034d9"]]},{"id":"fe645929.889508","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14.074,000","payloadType":"str","x":1580,"y":340,"wires":[["6f5ff02d.034d9"]]},{"id":"3fcbd991.7cfec6","type":"debug","z":"4118d274.45b1fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1970,"y":300,"wires":[]},{"id":"c0ae97cf.def198","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14,074.000","payloadType":"str","x":1570,"y":440,"wires":[["4536c6f2.b73c38"]]},{"id":"6a2d20d9.39cac","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14.074,000","payloadType":"str","x":1570,"y":520,"wires":[["4536c6f2.b73c38"]]},{"id":"d2e4af7d.0ac6a","type":"debug","z":"4118d274.45b1fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1950,"y":480,"wires":[]},{"id":"4536c6f2.b73c38","type":"function","z":"4118d274.45b1fc","name":"convert","func":"str = msg.payload;\nmsg.payload = str.replace('.', '#').replace(',', '.').replace('#', ',');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1760,"y":480,"wires":[["d2e4af7d.0ac6a"]]},{"id":"fceeb392.73143","type":"change","z":"4118d274.45b1fc","name":"JSONata","rules":[{"t":"set","p":"payload","pt":"msg","to":"$replace($replace($replace(msg.payload, '.', '#'), ',', '.'), '#', ',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1760,"y":680,"wires":[["7a8576d3.b8b358"]]},{"id":"c7821f65.05372","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14,074.000","payloadType":"str","x":1570,"y":640,"wires":[["fceeb392.73143"]]},{"id":"94368d61.0ffa7","type":"inject","z":"4118d274.45b1fc","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14.074,000","payloadType":"str","x":1570,"y":720,"wires":[["fceeb392.73143"]]},{"id":"7a8576d3.b8b358","type":"debug","z":"4118d274.45b1fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1970,"y":680,"wires":[]}]

Just pick the one you like and fits your flow.

Why no regex solution? Well, we had to leave you some homework… that’s the fun part about Node-RED, there are often so many different ways to do the same thing.

BTW, we even had to address this number formatting requirement in our MQTT client in groov Manage:

1 Like