Convert negative to positive and positive to negative


I have a payload number i need to convert form positive to negative or vice versa. tried to wrap my head around the math javascript but my coding skills are (non existing) are letting me down...

Any pointers will be appreciated.

Need the conversion to make my elec import/export numbers behave the right way!

I will answer my own question...

After a lot of struggle, this seems to be the simple solution:

var y = msg.payload;
var x = -y;
msg.payload = x;
return msg;

No need to create extra variables

msg.payload = msg.payload * -1;
return msg;

Omg! that simple!
Spend a morning searching to do so!


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