# Round() produces NaN

**URL:** <https://discourse.processing.org/t/round-produces-nan/30943>\
**Category:** p5.js\
**Created:** [June 28, 2021, 12:55pm UTC](https://discourse.processing.org/t/round-produces-nan/30943 "2021-06-28T12:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maze](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@maze](https://discourse.processing.org/u/maze)\
**Post date:** [June 28, 2021, 12:55pm UTC](https://discourse.processing.org/t/round-produces-nan/30943/1 "2021-06-28T12:55:47Z")

</div>

When rounding very small numbers to some decimal places, e.g round(1.234567e-14, 2) , the result is NaN.  
Not specifying decimals - round(1.234567e-14) - gives a correct result of 0.  
Does someone know if that is a wanted behaviour or a bug?

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [June 29, 2021, 1:34am UTC](https://discourse.processing.org/t/round-produces-nan/30943/2 "2021-06-29T01:34:20Z")

</div>

This is 💯 a bug 🐛. The implementation of the `round` function in `p5.js` is a horrible horrible idea 🙀:

```auto
p5.prototype.round = function(n, decimals) {
  if (!decimals) {
    return Math.round(n);
  }
  return Number(Math.round(n + 'e' + decimals) + 'e-' + decimals);
};

```

performing a multiplication and division by powers of 10 using string concatenation and conversion from string to Number is quite possibly the worst idea I’ve ever seen realized in javascript.

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [June 29, 2021, 1:42am UTC](https://discourse.processing.org/t/round-produces-nan/30943/3 "2021-06-29T01:42:44Z")

</div>

Submitted an issue: [The round() function does not work when a number of digits is specified with very small numbers. · Issue #5340 · processing/p5.js · GitHub](https://github.com/processing/p5.js/issues/5340)

---

<div class="post-metadata">

**Author:** ![maze](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@maze](https://discourse.processing.org/u/maze)\
**Post date:** [June 29, 2021, 9:36am UTC](https://discourse.processing.org/t/round-produces-nan/30943/4 "2021-06-29T09:36:13Z")

</div>

@KumuPaul Thank you very much for checking, replying, agreeing and submitting.
