Skip to content

fix: remove trailing zeros and handle zero and zero precision for %g in string/base/format-interpolate - #16538

Open
rajanpanth wants to merge 1 commit into
stdlib-js:developfrom
rajanpanth:fix/format-interpolate-g-specifier
Open

rajanpanth wants to merge 1 commit into
stdlib-js:developfrom
rajanpanth:fix/format-interpolate-g-specifier

Conversation

@rajanpanth

Copy link
Copy Markdown

Description

What is the purpose of this pull request?

This pull request:

  • fixes the %g/%G specifier in string/base/format-interpolate (and thus string/format), which only removed trailing zeros from exponential output and from a bare .0. For example, format( '%g', 1.5 ) returned '1.50000', rather than '1.5' as in C's printf, and format( '%g', 100 ) returned '100.000', rather than '100'.
  • formats zero as '0', rather than '0e+00'. Previously, every value with an absolute value less than 1e-4, including zero, used exponential notation.
  • treats a precision of zero as one significant digit, as in C. Previously, format( '%.0g', 1.5 ) threw RangeError: toPrecision() argument must be between 1 and 100.
  • chooses between fixed-point and exponential notation using the exponent X of the value rounded to P significant digits (fixed-point when P > X >= -4), which is the rule in the C standard. As a result, format( '%g', 0.0001 ) returns '0.0001' (previously '0.000100000') and format( '%g', 0.00009999995 ) returns '0.0001' (previously '1e-04').
  • keeps the decimal point for negative values in the alternate form (#). format( '%#.0f', -3 ) now returns '-3.', as it already did for positive values, and format( '%#.3g', -340.12 ) returns '-340.'.
  • adds tests for each of the above.
Format Value Before After (= C printf)
%g 1.5 1.50000 1.5
%g 100 100.000 100
%g 0 0e+00 0
%g 0.0001 0.000100000 0.0001
%.3g 0.1 0.100 0.1
%G 0.5 0.500000 0.5
%10g 1.5 1.50000 1.5
%.0g 1.5 throws 2
%#.3g -340.12 -340 -340.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

  • I compared the output against Python's % operator (which follows C's printf for floating-point conversions) for 5,950 combinations of 425 values (fixed and pseudo-randomly generated, spanning 1e-21 to 1e100) and 14 %g format strings (precision 0 through 17, #, +, -, 0, width, and %G). Before this change, 630 mismatched; after this change, 2 mismatch.
  • The 2 remaining mismatches are exact ties (e.g., format( '%.0g', -2.5 ) returns '-3', whereas C returns '-2'). C rounds exact ties to even, while Number.prototype.toExponential and Number.prototype.toFixed round ties away from zero. This also applies to %e and %f, predates this change, and is not addressed here.
  • The existing tests (including the %3.G and %#3.G cases) pass unchanged. I ran the tests for string/base/format-interpolate and string/format directly with Node.js and tape on Windows (as make was not available in my environment); I did not run the full repository lint pipeline locally.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written with the help of Claude Code, which found the issue, drafted the implementation change and tests, and ran the comparison described above. I reviewed the changes and the test results before submitting.


@stdlib-js/reviewers

🤖 Generated with Claude Code

…` in `string/base/format-interpolate`

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rajanpanth
rajanpanth requested a review from a team November 7, 2026 17:14
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. labels Nov 7, 2026
@kgryte
kgryte requested a review from Planeshifter November 8, 2026 08:37
@kgryte

kgryte commented Nov 8, 2026

Copy link
Copy Markdown
Member

@Planeshifter Mind taking a look at these changes since you were the one which originally added this functionality?

@kgryte kgryte added the difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. label Nov 8, 2026

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

difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants