# \[Solved\] Need help with math conversion

**URL:** https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661
**Category:** General
**Created:** [14 May 2021 18:59 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661 "2021-05-14T18:59:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![007bond](https://avatars.discourse-cdn.com/v4/letter/0/6de8d8/32.png) [@007bond](https://discourse.nodered.org/u/007bond)
#### Post date: [14 May 2021 18:59 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/1 "2021-05-14T18:59:51Z")

</div>

Sorry for the silly newbie questions.

OK so I am getting milliamps and I want amps displayed on my dashboard.

So I use a function node with this and all works great.  
var o = msg.payload  
msg.payload = msg.payload / 1000  
return msg;

Ok now the issue.  
I am also getting in watt hours and I want to display that as kWh on the dashboard.  
So the same math should work.  
var o = msg.payload  
msg.payload = msg.payload / 1000  
return msg;

But for some reason it does not work the dashboard still displays watt hours.

I don't understand why the math operation is not working?

Running debug on the mqtt output shows.  
5/14/2021, 2:36:04 PM[node: 1eeb439b.969b4c  
wallcharger/wh : msg.payload : string[6]  
"309794"

Running debug on the function node output shows nothing. This I think is telling me that the dashboard is not updating and it showing cached info and that nothing is flowing thru my function node.

I don't get it.

**OK so I did figure this out and I wanted to post in case someone else has the same issue.**

**I just decided to delete the function node and add it again. Then all of a sudden the debug node had output and popped in the formula and its working.**

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [14 May 2021 20:18 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/2 "2021-05-14T20:18:03Z")

</div>

> [@007bond](#):
>
> wallcharger/wh : msg.payload : string[6]  
> "309794"

This contains the clue. It is a string and not a number. While JavaScript isn't strongly typed like some languages, even so most Math will only work correctly on an actual number. You can also force a string to a number, there are a couple of different functions that will do that.

---

<div class="post-metadata">

### Author: ![007bond](https://avatars.discourse-cdn.com/v4/letter/0/6de8d8/32.png) [@007bond](https://discourse.nodered.org/u/007bond)
#### Post date: [14 May 2021 22:45 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/3 "2021-05-14T22:45:59Z")

</div>

IC thank you just for my education as I am new could you share in this example how I would go about changing the string to a number. Thanks again.

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [14 May 2021 22:56 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/4 "2021-05-14T22:56:00Z")

</div>

> **[Here, Let Me Google That For You](https://googlethatforyou.com/?q=javascript%20string%20to%20number)**
>
> Passive-aggressively teach your friends how to Google. For all those people who find it more convenient to ask you rather than search it themselves. Not associated with Google.

🙃

---

<div class="post-metadata">

### Author: ![007bond](https://avatars.discourse-cdn.com/v4/letter/0/6de8d8/32.png) [@007bond](https://discourse.nodered.org/u/007bond)
#### Post date: [14 May 2021 23:26 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/5 "2021-05-14T23:26:06Z")

</div>

Yea I deserved that one 🤣

---

<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: [28 May 2021 23:26 UTC](https://discourse.nodered.org/t/solved-need-help-with-math-conversion/45661/6 "2021-05-28T23:26:41Z")

</div>

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