European to North American Number Change - Solved

Is there a node, or suggested function Javascript to convert European formatted numbers to North American formatted numbers.

I have some Germans trying to use some flows I built, but the number formats are causing problems with "parseFloat" commands and the JSON parse node.

So, the flows I built in North America are not parsing correctly because of the different formats for floating numbers, i.e. when "14.074,000 (eu format) is "parseFloat" in a function, we get "14" instead of "14,074.000". So, Node-Red sees the "." as a decimal, instead of a "thousand" separator.

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

Alan

You could mess with the JavaScript Intl functions maybe.

Because you are using Parsefloat I'm assuming that the payload is a string.
The easiest way is to search and replace the ., with the other one.

[{"id":"f956991c.7acad8","type":"inject","z":"6ebb4e1a.90e7","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14.074,000","payloadType":"str","x":130,"y":1220,"wires":[["bd0a0af5.41dd4","dc96ab08.15e268"]]},{"id":"bd0a0af5.41dd4","type":"change","z":"6ebb4e1a.90e7","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":320,"y":1300,"wires":[["dc96ab08.15e268"]]},{"id":"dc96ab08.15e268","type":"debug","z":"6ebb4e1a.90e7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":1300,"wires":[]}]

However I doubt if this is the correct way. Normally the location of the decimal point and thousand separator is determined by the country settings of the operating system.
If this is set this correct for your country I think that parseFloat needs to work fine.

Solved. The "BaldGeek", Ben, on another forum came up with the solution.

Using a function node:

str = msg.payload;
msg.payload = str.replace('.', '#').replace(',', '.').replace('#', ',');
return msg;

And, using a change node:

[{"id":"6f5ff02d.034d9","type":"change","z":"6773a439.5cfddc","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":440,"y":2100,"wires":[["3fcbd991.7cfec6"]]},{"id":"cccb57ae.18de18","type":"inject","z":"6773a439.5cfddc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14,074.000","payloadType":"str","x":240,"y":2060,"wires":[["6f5ff02d.034d9"]]},{"id":"fe645929.889508","type":"inject","z":"6773a439.5cfddc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14.074,000","payloadType":"str","x":240,"y":2140,"wires":[["6f5ff02d.034d9"]]},{"id":"3fcbd991.7cfec6","type":"debug","z":"6773a439.5cfddc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":2100,"wires":[]}]

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