How to split decimals on a value

Hello,

I try to publish on a screen my electricity production but with the sensor that i use, i have on the screen a too big number with 2 decimals, for example 1835,48 W. I would like to split the decimals to show on the screen only 1835 W. I don't know how to do.. Does anybody know how i have to edit my script ?

var value = msg.payload;
var unit = msg.data.attributes.unit_of_measurement;
//msg.payload = price;

msg.payload = {
// "text": value + ' W',
"text": value + ' ' + unit,
"icon": 42453,
"duration": 4,
"repeat": 1,
"pushIcon": 3
};

node.status({ fill: 'green', shape: 'ring', text: value + ' ' + unit});

return msg;

I'm sure there are different ways of doing this but this is the way I do it. I add this line right before the return msg;
msg.payload = Math.trunc(msg.payload);

Could also use this to round down...
let value = Math.floor(msg.payload);

Or this to round up...
let value = Math.ceil(msg.payload);

Or this to round to nearest integer...
let value = Math.round(msg.payload);

Or this to convert a string to an integer, also works on numbers...
let value = parseInt(msg.payload);

1 Like

Also see this previous post -

1 Like

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