Get rid of the decimals in a json string.

7,191 views
Skip to first unread message

Marius Fosshagen Berget

unread,
Mar 15, 2017, 8:38:25 AM3/15/17
to Node-RED
I feed the node-red with a json string who looks like this:

N/b827eb79e8ef/system/0/Dc/Battery/Current : msg.payload : Object{ "value": 0.8999999761581421 }
I want to forward this to freeboard, but only with 2 decimals.. So the number will be in this case 0.89 or 0.90 (dosent matter if it rounds the number or just remove the decimals)..Have searched the internet and found alot of complex ways who I dont understand mutch of :)
Anyone who have a quick and easy solution?

Mark Setrem

unread,
Mar 15, 2017, 9:09:20 AM3/15/17
to Node-RED
try

msg.payload= msg.payload.value.toFixed(2);
return msg;

in a function node

steve rickus

unread,
Mar 15, 2017, 9:22:45 AM3/15/17
to Node-RED
Hi Marius,

The debug output shows your msg is an object, not a string -- and yes, there are many ways to do what you want, but it depends on what you want to pass to Freeboard. Since I'm not familiar with it, I'll just assume you only need the numeric value of the object, rounded to 2 decimal places.

Simplest solution for a javascript programmer would be this 3 line function node (which returns a string):

var numval = msg.payload.value || -1;
msg.payload = numval.toFixed(2);
return msg;


Marius Fosshagen Berget

unread,
Mar 15, 2017, 9:46:15 AM3/15/17
to Node-RED
And the problem i solved! 

When I searched the internet yesterday I was close, but no cigar!

If I ever meet any of you I`ll buy you a beer, thnx for the effort.

Colin Law

unread,
Mar 15, 2017, 10:00:54 AM3/15/17
to node...@googlegroups.com
On 15 March 2017 at 13:22, steve rickus <sri...@gmail.com> wrote:
> Hi Marius,
>
> The debug output shows your msg is an object, not a string -- and yes, there
> are many ways to do what you want, but it depends on what you want to pass
> to Freeboard. Since I'm not familiar with it, I'll just assume you only need
> the numeric value of the object, rounded to 2 decimal places.
>
> Simplest solution for a javascript programmer would be this 3 line function
> node (which returns a string):
>
> var numval = msg.payload.value || -1;

Won't that give a value of -1 if msg.payload.value is 0?

Colin

steve rickus

unread,
Mar 15, 2017, 10:11:20 AM3/15/17
to Node-RED
Why, yes, it would -- good catch! I tacked that on to be able to track missing values, but didn't test it.
I also didn't do any type checks/conversions for values that are NaN or strings, which would be good to add...

Colin Law

unread,
Mar 15, 2017, 10:34:58 AM3/15/17
to node...@googlegroups.com
I usually user Number() in these circumstances and then check isNaN()
and isFinite() to check for a valid number

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/0af45874-d5ba-409b-aa12-64ee14581683%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

fluri...@gmail.com

unread,
Mar 15, 2017, 11:03:55 AM3/15/17
to Node-RED
To get a float I usually do it this way: 
msg.payload.value = parseFloat((msg.payload.value ).toFixed(2));
 

Colin Law

unread,
Mar 15, 2017, 12:09:20 PM3/15/17
to node...@googlegroups.com
I am not very familiar with freeboard, but does it not have display
formatting capability? If so then it is probably better to format it
at display time in freeboard rather than throwing away accuracy before
you need to.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/e077148f-5be8-45a4-95d1-1e1dd638971e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages