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

**URL:** <https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013>\
**Category:** General\
**Created:** [3 November 2022 11:18 UTC](https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013 "2022-11-03T11:18:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [3 November 2022 11:18 UTC](https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013/1 "2022-11-03T11:18:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [3 November 2022 11:25 UTC](https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013/2 "2022-11-03T11:25:01Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [3 November 2022 11:35 UTC](https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013/3 "2022-11-03T11:35:26Z")

</div>

> [@knolleary](#):
>
> ```auto
> 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. 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 November 2022 11:35 UTC](https://discourse.nodered.org/t/how-can-i-convert-string-2-7777777777777776e-7-to-number/70013/4 "2022-11-17T11:35:38Z")

</div>

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