How to do proper number calculations in a Payload Format decoder?

Looks good to me. :slight_smile:

Just for the sake of completeness: above you’re not removing any decimals that are introduced by JavaScript. Instead, this code expects the node to have multiplied by 100 to preserve 2 decimals, while the application apparently only wants 1.

Note that using toFixed will round the value, so 7.95 will become 8.0. When using this on errors introduced by JavaScript calculations, in theory the result might slightly differ from what has been sent by the device, but I doubt one will ever run into that.

Aside, there’s a shorter way, which surely is harder to read for those who don’t know the unary plus operator:

// Unary plus operator to cast string result of toFixed to number
decoded.tempC = +((bytes[4]<<24>>16 | bytes[5]) / 100).toFixed(1);
1 Like