processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.1k stars 3.22k forks source link

`nf()` produces problematic string-formatting of negative numbers #7046

Open nickmcintyre opened 1 month ago

nickmcintyre commented 1 month ago

Most appropriate sub-area of p5.js?

p5.js version

1.9.3

Web browser and version

Chrome 124.0.6367.208

Operating system

macOS 14.4.1

Steps to reproduce this

Steps:

  1. Call nf() with a negative number and extra left padding.
  2. Display the formatted number.

Snippet:

function setup() {
  createCanvas(400, 400);

  background(220);

  // Format a negative number with
  // extra left padding.
  let num = nf(-123, 5);

  // Style the text.
  textAlign(CENTER, CENTER);
  textSize(32);

  // Display the text.
  text(num, 200, 200); // 0-123
}

Here's the sketch for reference.

After reviewing #5710, it may be worth revisiting the string utility functions as part of p5.js 2.0.