Skip to content

fix: format a negative value that rounds to zero like any other zero - #423

Open
bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix/negative-zero-formatting
Open

bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix/negative-zero-formatting

Conversation

@bardrop

@bardrop bardrop commented Nov 17, 2026

Copy link
Copy Markdown

Summary

A negative value that rounds to zero is passed to applyNumberFormatting as the string "-0" (so the sign survives). That string skipped the normal zero formatting:

  • with pad + separator, the rounding step turned "-0" into the number -0, and (-0).toString() is "0", so the sign was lost: filesize(-1, { exponent: 2, pad: true, separator: "," }) gave "0,00 MB";
  • with a locale, "-0" stayed a string, and String.prototype.toLocaleString ignores the locale and options, so padding, localeOptions and the locale's minus sign were dropped: filesize(-1, { exponent: 2, pad: true, locale: "de" }) gave "-0 MB" while filesize(1, …) gives "0,00 MB".

Now "-0" is parsed to -0 in the locale branch (Intl prints negative zero with the locale's own sign, e.g. "−0" for sv, same as other negatives), and the separator rounding step is skipped for it (rounding zero changes nothing). Non-locale output without a separator is unchanged ("-0.00 B"). Zero strings from precision (e.g. "0.00") keep their current handling.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Related Issues

Fixes #422

Testing

New test "should format a negative value that rounds to zero like any other zero" in tests/unit/filesize.test.js (pad + separator, pad + de, localeOptions, sv, system locale). It fails on master ('0,00 MB' vs '-0,00 MB') and passes with the change. npm test: 285 pass, lint + format clean, 100% coverage. npm run build succeeds (dist not committed).

Checklist

  • npm test passes
  • npm run build succeeds
  • 100% test coverage maintained
  • No hardcoded secrets or credentials introduced
  • Zero external dependencies added
  • ES Modules only (no CommonJS in src/)
  • JSDoc comments added/updated (no signature change)
  • CHANGELOG.md updated (if applicable) (auto-changelog)

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: a negative value that rounds to zero ignores locale, padding and separator sign

1 participant