Skip to content

[p5.js 2.0+ Bug Report]: nfc() truncates instead of rounding and drops the documented trailing zero padding #9125

Description

@rajanpanth

Most appropriate sub-area of p5.js?

Utilities

p5.js version

2.x main (4b096e2)

Actual vs expected behavior

nfc()'s own reference examples document rounding and zero padding, but doNfc in src/utilities/utility_functions.js compares the requested decimal count against a substring that still contains the leading ., so it truncates where the docs round and skips the padding branch by one:

nfc(12345.67, 1); // actual '12,345.6'   documented '12,345.7'
nfc(12345.67, 3); // actual '12,345.67'  documented '12,345.670'

nf() already rounds via toFixed, so the two formatters also disagree with each other on the same input. A rounding carry also needs comma re-grouping, e.g. nfc(999.96, 1) should give '1,000.0'.

Steps to reproduce

Outputs above are from executed runs against current main via the unit test harness (npx vitest run test/unit/utilities/utility_functions.js with added cases).

Note

I have a fix ready (route through toFixed like nf, keep the comma grouping, preserve the existing nfc(32000, '3') === '32,000.000' behavior) with unit tests covering rounding, padding, the carry re-group and negatives. Verified the four new tests fail on current main and pass with the change, 23 existing tests unaffected. Filing for approval first per the contributing guide; will open the PR once approved.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions