# Percent calculation

**URL:** <https://discourse.nodered.org/t/percent-calculation/25309>\
**Category:** General\
**Created:** [22 April 2020 15:05 UTC](https://discourse.nodered.org/t/percent-calculation/25309 "2020-04-22T15:05:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cedric](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cedric](https://discourse.nodered.org/u/Cedric)\
**Post date:** [22 April 2020 15:05 UTC](https://discourse.nodered.org/t/percent-calculation/25309/1 "2020-04-22T15:05:37Z")

</div>

Hi

I would calculat the percent value of 2 msg.payload, but I get only NaN value.

Could somebody explain me, how I can calculate percent in nodeRED ?

Thank you

[percent.json](https://discourse.nodered.org/uploads/short-url/zl373aTn8kLKW1AJQ2a8FjHQ32q.json) (485 Bytes)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 April 2020 15:25 UTC](https://discourse.nodered.org/t/percent-calculation/25309/2 "2020-04-22T15:25:58Z")

</div>

You have

```auto
var income = flow.get('income');
var outgoing = flow.get('outgoing');
msg.payload = ( 100 / ('income') * ( ('outgoing')));
//(100 / income * outgoing);//or what ever kind of formula you want
//Math.round(100/Number(context.ylekastmine)*(Number(context.spoon)));
return msg;

```

The third line should not have income and outgoing in quotes, otherwise your are trying to use the string 'income' rather than the contents of that variable. So it should be (reorganising slightly to make the calcs a bit clearer too)  
`msg.payload = 100 * outgoing / income`

---

<div class="post-metadata">

**Author:** ![Cedric](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cedric](https://discourse.nodered.org/u/Cedric)\
**Post date:** [22 April 2020 15:49 UTC](https://discourse.nodered.org/t/percent-calculation/25309/3 "2020-04-22T15:49:01Z")

</div>

> [@Colin](#):
>
> The third line should not have income and outgoing in quotes, otherwise your are trying to use the string 'income' rather than the contents of that variable. So it should be (reorganising slightly to make the calcs a bit clearer too)

Thank you very much. But I get still NaN.

---

<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:** [22 April 2020 15:59 UTC](https://discourse.nodered.org/t/percent-calculation/25309/4 "2020-04-22T15:59:45Z")

</div>

Hi @Cedric

The Function node you have shared assumes you have already put the two values into flow context before the Function is triggered.

If you are getting `NaN` then one or both of your values is probably not set properly.

Can you share your wider flow that shows how `income` and `outgoing` are set in context, and how you are triggering this Function node?

---

<div class="post-metadata">

**Author:** ![Cedric](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cedric](https://discourse.nodered.org/u/Cedric)\
**Post date:** [22 April 2020 16:44 UTC](https://discourse.nodered.org/t/percent-calculation/25309/5 "2020-04-22T16:44:28Z")

</div>

Now, I get the solution.

The error was that I set 2times the msg.payload.

Thank you very much.

var maxHeight = flow.get('income');  
var height = msg.payload;

msg.payload = height / maxHeight \* 100;

---

<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:** [21 June 2020 16:53 UTC](https://discourse.nodered.org/t/percent-calculation/25309/6 "2020-06-21T16:53:59Z")

</div>

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