Delete the decimals

Hello,
I want to get rid of the decimals.
My function node is like this:

gas = parseFloat(msg.payload.svalue1);
msg.payload = gas;
msg.topic = "gas";
return msg;

The outcome data is like:
gas : msg.payload : number
2437.604

or sometimes 2450.50

or sometimes 2730.3

I only want to use the whole numbers (integers).

How can i change the function code to only get the numbers before the decimal point?

You can use toFixed if you want a fixed number of DP's. Just be aware that it turns the number into text though.

Number.prototype.toFixed() - JavaScript | MDN (mozilla.org)

I would do a google search using 'javascript how to strip decimals from number'

Here is an example in a function node

msg.payload = Math.trunc(msg.payload)
return msg;

well you could just use parseInt instead of paareFloat...
but maybe you want to round to nearest integer instead in which case you could use Math.round(gas).

1 Like

Thank you all. All sugestions were helpfull. Finanaly I used the Math.round code and it is working very well.

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