# Need some help with float numbers

**URL:** <https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292>\
**Category:** General\
**Created:** [12 July 2021 08:42 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292 "2021-07-12T08:42:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 08:42 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/1 "2021-07-12T08:42:18Z")

</div>

Hi,

I have some difficulties with float numbers. I am usind node-red modbus to read some modbus values. I read two values and they are in array [34853, 200]. I need to convert that first number to negative float. Can someone help ? also I return two values from one output.

```auto
var msg1 = {payload: msg.payload[0], topic: "RW4010"};
var msg11;
if (msg1 > 32767) {
  msg11 = -65536 + msg1;
} else if (msg1 <= 32767) {
  msg11 = msg1;
}
var msg2 = {payload: msg.payload[1], topic: "RW4011"};
var msg21;
if (msg2 > 32767) {
  msg21 = -65536 + msg2;
} else if (msg1 <= 32767) {
  msg21 = msg2;
}
return [[msg11, msg21] ];

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2021 09:21 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/2 "2021-07-12T09:21:08Z")

</div>

> [@Aivaras](#):
>
> [34853, 200]. I need to convert that first number to negative float. Can someone help ? also I return two values from one output.

As modbus returns 16bit number, that "first number" is a 16bit (2 byte) number. A float is 32bit (4 bytes) so I presume the manual for your modbus device will give you a formula for converting a 2 byte value into a float.

My guess is you should be reading an int (not UINT) and then probably divide by 10 or 100 or 1000

e.g. - using [buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)...

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

demo flow

```auto
[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2100,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2070,"y":600,"wires":[["16902e50e15dced7"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":600,"wires":[]}]

```

* * *

alternative version sending multiple messages from one output...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf78d866b331de44b854969a8875a202a4f20450.png)

```auto
[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2100,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2070,"y":600,"wires":[["16902e50e15dced7"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":600,"wires":[]}]

```

* * *

Another alternative version - results to own outputs (fan out)

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

```auto
[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2040,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":4,"x":2050,"y":640,"wires":[["16902e50e15dced7"],["575a304c4ce43036"],["f23cf12dcee6af06"],["10843da0954e4d4e"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":580,"wires":[]},{"id":"575a304c4ce43036","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":620,"wires":[]},{"id":"f23cf12dcee6af06","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":660,"wires":[]},{"id":"10843da0954e4d4e","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":700,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 10:01 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/3 "2021-07-12T10:01:15Z")

</div>

Thank you for info Steve, but maybe you know how to do this in function block ? 😄

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2021 10:06 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/4 "2021-07-12T10:06:01Z")

</div>

Yes, but i dont because there is a contrib node to do it for me.

Do you not want to use buffer-parser for some reason?

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 10:09 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/5 "2021-07-12T10:09:26Z")

</div>

Yes, because I want to put this to one function. Optimizing a node-red flow little bit.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2021 10:12 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/6 "2021-07-12T10:12:05Z")

</div>

> [@Aivaras](#):
>
> Optimizing a node-red flow little bit

[Premature optimisation is the root of all evil](https://stackify.com/premature-optimization-evil/)

I gave you a solution that works - if it is a problem - you can later optimise.

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 10:29 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/7 "2021-07-12T10:29:55Z")

</div>

Thank you for your help Steve 😃

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2021 10:31 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/8 "2021-07-12T10:31:06Z")

</div>

> [@Steve-Mcl](#):
>
> Premature optimisation is the root of all evil

And here is proof...

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

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 11:12 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/9 "2021-07-12T11:12:21Z")

</div>

But maybe you can show what it is inside those function blocks ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 July 2021 11:22 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/10 "2021-07-12T11:22:22Z")

</div>

> [@Aivaras](#):
>
> But maybe you can show what it is inside those function blocks

Sure, but like I said, there is already a working solution (3 of them) in my first post that is as fast as (and often faster) than DIY solution.

## Here you go anyway...

for the functions below to work, you need to get the path to the buffer (usually something like `msg.responseBuffer.buffer` or something like that.

### Output a single object...

```auto
/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

msg.payload = {
    myFloat_div10: int1 / 10,
    myFloat_div100: int1 / 100,
    myFloat_div1000: int1 / 1000,
    myNumber: int2
}

return msg;

```

### output stream of 4 messages...

```auto
/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

node.send({ topic: "myFloat_div10", payload: int1 / 10});
node.send({ topic: "myFloat_div100", payload: int1 / 100});
node.send({ topic: "myFloat_div1000", payload: int1 / 1000});
node.send({ topic: "myNumber", payload: int2});

```

### fan out 4 messages...

```auto
/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

var m1 = { topic: "myFloat_div10", payload: int1 / 10};
var m2 = { topic: "myFloat_div100", payload: int1 / 100};
var m3 = { topic: "myFloat_div1000", payload: int1 / 1000};
var m4 = { topic: "myNumber", payload: int2};

node.send([m1, m2, m3, m4]);

```

---

<div class="post-metadata">

**Author:** ![Aivaras](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aivaras](https://discourse.nodered.org/u/Aivaras)\
**Post date:** [12 July 2021 11:25 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/11 "2021-07-12T11:25:36Z")

</div>

Thank you for your help 😃

---

<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:** [10 September 2021 11:26 UTC](https://discourse.nodered.org/t/need-some-help-with-float-numbers/48292/12 "2021-09-10T11:26:15Z")

</div>

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