# Rounding numbers in array with jsonata through change node

**URL:** <https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253>\
**Category:** General\
**Created:** [9 October 2024 04:17 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253 "2024-10-09T04:17:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [9 October 2024 04:17 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/1 "2024-10-09T04:17:53Z")

</div>

Hi all. I need to round certain variables in a data feed that's coming in the following format:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/57f4f3b0682df3c79278155b07ba32beac468f8e.png)

For instance the "gap" variable to 2 decimal places. It seems this can be done simply using jsonata's $round function through the change node. I can't figure out how to address individual variables to process the entire array and get it out in the same format. Thanks so much!

```auto
[{"ticker":"NASDAQ:LASE","name":"LASE","close":7.93,"gap":-3.7037037037,"relative_volume_10d_calc":20.8748866128,"change":83.5648148148,"premarket_change":-1.8518518519,"float_shares_outstanding":3858570.0383889996,"change_from_open":90.625},{"ticker":"NASDAQ:OTRK","name":"OTRK","close":2.76,"gap":64.8351648352,"relative_volume_10d_calc":461.2613085129,"change":51.6483516484,"premarket_change":65.3846153846,"float_shares_outstanding":1248683.6766000001,"change_from_open":-8},{"ticker":"AMEX:BURU","name":"BURU","close":1.01,"gap":33.4824424066,"relative_volume_10d_calc":58.3945386478,"change":125.90024603,"premarket_change":29.9485573697,"float_shares_outstanding":3556713.4752,"change_from_open":69.235924933},{"ticker":"NASDAQ:BPTH","name":"BPTH","close":1.19,"gap":50.3582755785,"relative_volume_10d_calc":739.1336072032,"change":39.7862093269,"premarket_change":50.3582755785,"float_shares_outstanding":2550487.68927,"change_from_open":-7.03125},{"ticker":"AMEX:RHE","name":"RHE","close":2.48,"gap":12.6373626374,"relative_volume_10d_calc":1352.2586177328,"change":36.2637362637,"premarket_change":14.2857142857,"float_shares_outstanding":1699040.22617,"change_from_open":20.9756097561},{"ticker":"NASDAQ:XRTX","name":"XRTX","close":1.8,"gap":-0.6535947712,"relative_volume_10d_calc":7.2865541922,"change":17.6470588235,"premarket_change":null,"float_shares_outstanding":2698095.96538,"change_from_open":18.4210526316},{"ticker":"OTC:RLFTF","name":"RLFTF","close":4.91,"gap":25.1445086705,"relative_volume_10d_calc":7.2145669291,"change":41.9075144509,"premarket_change":null,"float_shares_outstanding":8255497.121424,"change_from_open":13.3949191686}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2024 05:31 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/2 "2024-10-09T05:31:36Z")

</div>

You could use an expression like:

```auto
$$.payload.{
      
    "ticker": $.ticker,
    "name": $.name,
    "close": $.close,
    "gap": $round($.gap,2),
    "relative_volume_10d_calc": $round($.relative_volume_10d_calc,2),
    "change": $round($.change,2),
    "premarket_change": $.premarket_change ? $round($.premarket_change,2) : 0,
    "float_shares_outstanding": $round($.float_shares_outstanding,2),
    "change_from_open": $round($.change_from_open,2)
  
}

```

note that the 'premarket\_change' null values will be changed to 0.  
pretty sure that the jsonata wizards may have better solutions.

See [https://try.jsonata.org/h1ECOlq3A](https://try.jsonata.org/h1ECOlq3A)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 October 2024 05:44 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/3 "2024-10-09T05:44:18Z")

</div>

This is not going to work as the expression is mapping the $$ (msg) property. The issue is you did not set the JSONate exerciser input object correct. It needed to be

```auto
{
  "payload": [
    {...},
    {...}
  ]
}

```

Then the expression would  
start  
`$$.payload.{....`

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2024 05:45 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/4 "2024-10-09T05:45:57Z")

</div>

> [@bakman2](#):
>
> jsonata wizards

I explicitly mentioned you 😉

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [9 October 2024 05:49 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/5 "2024-10-09T05:49:50Z")

</div>

So what would you advise? This is the format that's currently working with my table config? Do I need to transform the data prior to passing it to the change node? Should it just be done through a function?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 October 2024 05:52 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/6 "2024-10-09T05:52:33Z")

</div>

Use @bakman2 expression just add payload ( or the correct property as your image obsures this information) to the beginning.

Or  
depending on the table node you could use the table to round the information it receives

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2024 05:52 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/7 "2024-10-09T05:52:36Z")

</div>

I have modified my answer.

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [9 October 2024 17:21 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/8 "2024-10-09T17:21:08Z")

</div>

Perfect, thanks! Just one minor issue; I have a variable called "relative\_volume\_10d\_calc|5" ... getting an error because of the "|".

P.S. originally I was doing the rounding in the table, but I needed to apply other formatting as well and tabulator doesn't seem to allow multiple types of formatting (only via custom functions).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [9 October 2024 17:39 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/9 "2024-10-09T17:39:40Z")

</div>

suround it with backticks

```auto
`relative_volume_10d_calc|5`

```

---

<div class="post-metadata">

**Author:** ![sonificator](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sonificator](https://discourse.nodered.org/u/sonificator)\
**Post date:** [9 October 2024 17:46 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/10 "2024-10-09T17:46:42Z")

</div>

> [@E1cid](#):
>
> `relative_volume_10d_calc|5`

Thank you!

---

<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:** [23 October 2024 17:46 UTC](https://discourse.nodered.org/t/rounding-numbers-in-array-with-jsonata-through-change-node/92253/11 "2024-10-23T17:46:54Z")

</div>

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