Number in German format in dashboard (100.235,25) - how to?

How can I format my msg.payload to be shown in German format in dashboard text node.
not 100,235.25
but 100.235,25
for example.
Any help appreciated.

you could convert it to a string beforehand and do a regexreplace in a function node.
Have a look at this function node:

[{"id":"2d6bb7f8.73721","type":"function","z":"c7988e96.f0bac","name":"","func":"if(typeof msg.payload != \"string\") msg.payload = String(msg.payload);\nif(msg.payload.match(/\\./g)) {\n    msg.payload = msg.payload.replace(/\\,/g, \".\");\n    msg.payload = msg.payload.replace(/\\.(?!.*\\.)/g, \",\");\n} else {\n    msg.payload = msg.payload.replace(/\\,/g, \".\");\n}\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":500,"wires":[["6ae79d5c.bc65a4"]]}]

it checks if payload is a string and else converts it to a string.
it than checks if the string contains a “.” (so it contains decimal places)
it than replaces all “,” with a “.” and the last “.” with a comma using a negative look ahead.
Else if the english format number string doesn’t have any decimal places it just replaces any “,” with “.”.
Hope this helps, Johannes.

many thanks for your support. It works.
I always thought, I could fix it with Angular filters.
There is still a little problem. I am using a gauge to show current solar power. How can I change the dot to comma within the gauge?

Have a look at this:

[{"id":"47ab2415.2c4624","type":"ui_gauge","z":"2e26d7ec.3b45c","name":"","group":"28d9dd4.23e65a2","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{msg.numstring}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1630,"y":700,"wires":[]},{"id":"41c03c3b.86a264","type":"inject","z":"2e26d7ec.3b45c","name":"","topic":"","payload":"6.7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1310,"y":700,"wires":[["a244754.c025788"]]},{"id":"a244754.c025788","type":"function","z":"2e26d7ec.3b45c","name":"","func":"let convert = msg.payload;\nif(typeof convert != \"string\") convert = String(convert);\nif(convert.match(/\\./g)) {\n    convert = convert.replace(/\\,/g, \".\");\n    convert = convert.replace(/\\.(?!.*\\.)/g, \",\");\n} else {\n    convert = convert.replace(/\\,/g, \".\");\n}\nmsg.numstring = convert;\nreturn msg;","outputs":1,"noerr":0,"x":1470,"y":700,"wires":[["47ab2415.2c4624"]]},{"id":"28d9dd4.23e65a2","type":"ui_group","z":"","name":"Wohnung","tab":"93e51f49.a58cd8","order":1,"disp":false,"width":"6","collapse":false},{"id":"93e51f49.a58cd8","type":"ui_tab","z":"","name":"Übersicht","icon":"info","order":1,"disabled":false,"hidden":false}]

I changed the function node a little bit. It now assigns the converted value string to a new msg property msg.numstring instead of of overwriting the msg.payload.
It than uses the msg.numstring value as a value format in the gauge node in the form of {{msg.numstring}}.
This should do what you want.

Thank you very much! It works!

1 Like

this was not about date time strings but actual number values. this only works for date objects as far as i can see. The peculiarity here is that in Germany we use a “,” instead of a “.” do show decimal places in numbers but all number values in programming languages are inherently in an english format.
Edit i see it works for numbers too

Oops, read to quickly. BTW, I lived in Germany for 33 years :slight_smile:

1 Like

Actually, it does work for numbers too!

Hmm, I cant get it too work somehow:

var a = 66.5;
var b = a.toLocaleString('de-DE');

works in the browser but not in nodered

I just tried too, didn't work either!

1 Like

So the regex way i proposed seams to be the preferable way still.
would have been so much tidier with toLocaleString() but for some reason it doesnt work in nodered.

so it seams to be node.js issue:

for future reference:
You have to first install 'intl' with npm according to the nodered manual ( so that you can use it in nodered functions.
You have to npm install intl from the same directory as your settings.js file and add it to the function global context in the settings.js file like this:

functionGlobalContext: {

and afterwards restart nodered.
When this is done you can use it like this in a function node:

var intl = global.get('intl');
let a = msg.payload;
let b = intl.NumberFormat('de-De',{style:'decimal'}).format(a);
msg.payload = b;
return msg;

The input has to be of type number.
maybe just use regex otherwise :see_no_evil:
Best regards Johannes

1 Like

actually I was trying to use intl.NumberFormat but with no success.
Thanks to your post above, I think that I will manage to make it work in Node-Red.
Perfect job, thanks!

Ok, I implemented intl and it works perfectly as long as it is de-DE decimal. Using de_DE currency does not get a comma. So I used decimal and put an € sign in front of msg.payload in the Text-node.

1 Like

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