Convert negative to positive and positive to negative

Hello,

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;
4 Likes

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

thanks!

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