Round() produces NaN

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?

1 Like

This is :100: a bug :bug:. The implementation of the round function in p5.js is a horrible horrible idea :scream_cat::

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.


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


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