How can I convert string 2.7777777777777776e-7 to number?

My boat engine for some reason insists on sending fuel consumption in cubic meters per second over NMEA2000 to SignalK, which then gets MQTT'ed to Node-RED, which gives me a ridiculous number as 2.7777777777777776e-7 when I'm using 1,7 liters per hour (which is idle)!

I tried a quick function node:

var forbruk = msg.payload*6000000;
var avrundetforbruk = parseFloat(forbruk).toFixed(2);
msg.payload = avrundetforbruk;
return msg;

But that didn't work, I don't think the normal way of changing stuff to numbers work. I also tried a change node with this in:

$number(payload)

But that didn't seem to understand the idiotic number either. How do I convert this in the simplest way?

Hi @Mastiff

The first line of your function you are trying to treat msg.payload as a number, but it is still a String at that point. You need to move your parseFloat up:

var forbruk = parseFloat(msg.payload)*6000000;
msg.payload = forbruk.toFixed(2);
return msg;

Thanks! I see, I was thinking Python, there you can convert a string to integer by multiplying or dividing... Embarassing. Working now. :+1:

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