p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

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

Open rajanpanth opened this issue 3 months ago • 2 comments

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.

rajanpanth avatar Sep 02 '26 16:09 rajanpanth

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms.

For guidance on contributing, check out our contributing guidelines and other resources for contributors. 💬 If you have questions or need support, feel free to join the Processing Foundation Forum or visit our Community page. 📜 Please also review our Code of Conduct to understand our community standards. Thank You!

welcome[bot] avatar Sep 02 '26 16:09 welcome[bot]

@ksen0 i would like to know if this issue is still relevant to current version , If yes then I would like to work on it

kaifansariw avatar Nov 11 '26 19:11 kaifansariw