# How to do proper number calculations in a Payload Format decoder?

**URL:** <https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799>\
**Category:** Application Development\
**Created:** [December 19, 2019, 4:13pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799 "2019-12-19T16:13:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas-parry](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thomas-parry/32/25844_2.png) [@thomas-parry](https://www.thethingsnetwork.org/forum/u/thomas-parry)\
**Post date:** [December 19, 2019, 4:13pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/1 "2019-12-19T16:13:54Z")

</div>

Im decoding lat and long passing the example of : 54 C9 D0 4F 68 C4 18 C0 FF , the moment i subtract 90/180 it adds more decimals than needed ? realistically it should just stop at 52.2512207 anyway i can stop this or the reason behind this , im not familiar with javaSCRIPT

thanks for any help 🙂

![TTN Console](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/e/0/e0e283c16a5afc1b80c1b4adcd0c91bf63611a03.png)

---

<div class="post-metadata">

**Author:** ![cslorabox](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cslorabox/32/35697_2.png) [@cslorabox](https://www.thethingsnetwork.org/forum/u/cslorabox)\
**Post date:** [December 19, 2019, 6:52pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/2 "2019-12-19T18:52:36Z")

</div>

It’s javascript, not java

Have a look at

> <https://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript>

However its worth noting that the execution environment is not the same as typical javascript targets let a web browser, etc.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [December 19, 2019, 7:34pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/3 "2019-12-19T19:34:23Z")

</div>

There are quite a few things going on here:

- Like @cslorabox already noted: JavaScript is not the same as Java. The payload formats in TTN Console use JavaScript (ECMAScript 5 to be precise). See also [MDN Web Docs](https://developer.mozilla.org/) to learn that.

- **JavaScript’s decimal numbers are not exact.**

- Apparently your device’s code is converting the decimal coordinates into positive integer numbers. However, when using bitwise operators in your decoder, then [JavaScript expects _signed_ 32 bits numbers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers) using [two’s complement encoding](https://en.wikipedia.org/wiki/Two%27s_complement), hence allowing for numbers from -2,147,483,648 through 2,147,483,647.

Finally, next time please don’t post screenshots for text: even after I cropped the image it’s hard to read on small devices, and it’s impossible to copy or quote. See [How do I format my forum post? [HowTo]](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post-howto/8735) Also, I doubt the output shown for longitude goes with the code that is shown…?

---

<div class="post-metadata">

**Author:** ![cultsdotelecomatgmai](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cultsdotelecomatgmai/32/35697_2.png) [@cultsdotelecomatgmai](https://www.thethingsnetwork.org/forum/u/cultsdotelecomatgmai)\
**Post date:** [December 19, 2019, 7:53pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/4 "2019-12-19T19:53:17Z")

</div>

Hi @thomas-parry, the following is an example of the JavaScript decoder code that I use to prevent runaway decimal digits:

```auto
 decoded.tempC = Number(((bytes[4]<<24>>16 | bytes[5]) / 100).toFixed(1));

```

This code results in reported values having one decimal digit, such as:

```auto
"tempC": 7.9

```

I’m a very amateur JavaScript programmer so I expect that purists will not approve and will hopefully show better ways of doing this.

---

<div class="post-metadata">

**Author:** ![thomas-parry](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thomas-parry/32/25844_2.png) [@thomas-parry](https://www.thethingsnetwork.org/forum/u/thomas-parry)\
**Post date:** [December 19, 2019, 8:23pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/5 "2019-12-19T20:23:55Z")

</div>

Thanks man , yeah the output doesn’t match cause i was having a try at figuring it out , sorry i don’t know javascript currently only know C/C++ but onwards and upwards

I’ll give all you have said a go 🙂

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [December 19, 2019, 8:48pm UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/6 "2019-12-19T20:48:58Z")

</div>

> [@cultsdotelecomatgmai](#):
>
> ```javascript
> decoded.tempC = Number(((bytes[4]<<24>>16 | bytes[5]) / 100).toFixed(1));
> 
> ```

Looks good to me. 🙂

Just for the sake of completeness: above you’re not removing any decimals that are introduced by JavaScript. Instead, this code _expects_ the node to have multiplied by 100 to preserve 2 decimals, while the application apparently only wants 1.

Note that using `toFixed` will round the value, so 7.95 will become 8.0. When using this on errors introduced by JavaScript calculations, _in theory_ the result might slightly differ from what has been sent by the device, but I doubt one will ever run into that.

Aside, there’s a shorter way, which surely is harder to read for those who don’t know [the unary plus operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_2):

```javascript
// Unary plus operator to cast string result of toFixed to number
decoded.tempC = +((bytes[4]<<24>>16 | bytes[5]) / 100).toFixed(1);

```

---

<div class="post-metadata">

**Author:** ![cultsdotelecomatgmai](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cultsdotelecomatgmai/32/35697_2.png) [@cultsdotelecomatgmai](https://www.thethingsnetwork.org/forum/u/cultsdotelecomatgmai)\
**Post date:** [December 20, 2019, 7:46am UTC](https://www.thethingsnetwork.org/forum/t/how-to-do-proper-number-calculations-in-a-payload-format-decoder/32799/7 "2019-12-20T07:46:33Z")

</div>

Thanks for the unary plus operator information - typical amateur sub-optimal programming on my part. My main professional programming experience was 3 years of Fortran77 more than 30 years ago. Sadly TTN doesn’t offer a Fortran option for decoders.
