# Rounding a value to another value from a pre-defined table

**URL:** <https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132>\
**Category:** General\
**Created:** [13 December 2022 14:36 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132 "2022-12-13T14:36:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [13 December 2022 14:36 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132/1 "2022-12-13T14:36:59Z")

</div>

Greetings. I am familiar with the various options and tools in Node-RED to do rounding of integers and floats, but can something like this be done?

Let's assume I record an integer between 957 and 1021. Can one create a function whereby the value is rounded (up or down) to the nearest of the 4 values shown below?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d89f46ae4e949da5a91541b0411ff3fc9334b3ea.png)

---

<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:** [13 December 2022 15:36 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132/2 "2022-12-13T15:36:32Z")

</div>

Something like this

```auto
//const values = [1021,1007,989,957]; //if equal round down
const values = [957,989,1007,1021]; //if equal round up
const output = {};
for(const num of values){
    output[Math.abs(msg.payload - num)] = num;
}
const minimum = Math.min(...Object.keys(output));
msg.payload = output[minimum];
return msg;

```

---

<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:** [13 December 2022 16:28 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132/3 "2022-12-13T16:28:07Z")

</div>

Or this is another solution in a few less lines

```auto
const values = [1021, 1007, 989, 957];
const value = msg.payload
const closest = values.reduce(function (prev, curr) {
    return (Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
});
msg.payload = closest
return msg;

```

Well one less anyway.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [13 December 2022 18:15 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132/4 "2022-12-13T18:15:46Z")

</div>

Thank you both! These work great for my needs.

---

<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:** [27 December 2022 18:16 UTC](https://discourse.nodered.org/t/rounding-a-value-to-another-value-from-a-pre-defined-table/72132/5 "2022-12-27T18:16:05Z")

</div>

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