Skip to content

Use exponential notation for rendering Amount values#104

Open
jessealama wants to merge 17 commits intomainfrom
flesh-out-tostring-exponential
Open

Use exponential notation for rendering Amount values#104
jessealama wants to merge 17 commits intomainfrom
flesh-out-tostring-exponential

Conversation

@jessealama
Copy link
Copy Markdown
Collaborator

@jessealama jessealama commented Apr 29, 2026

Resolves the toString TODO with a new AO, RenderInExponentialNotation(value, sigDigits), which renders a mathematical value in canonical exponential notation.

BigInt rendering here doesn't strip trailing zeros, so 100000n now serializes as "1.00000e+5", which makes things uniform with how Numbers and Strings are handled.

An editorial note records that the AO does essentially overlaps with Number.prototype.toExponential and that the two should eventually share an extracted operation.

@jessealama jessealama marked this pull request as draft April 29, 2026 08:26
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://tc39.github.io/proposal-amount/pr-preview/pr-104/

Built to branch gh-pages at 2026-05-05 13:37 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@jessealama jessealama changed the title [spec] Render Amount values in canonical exponential notation Store and render Amount values in exponential notation Apr 29, 2026
@jessealama jessealama changed the title Store and render Amount values in exponential notation Use exponential notation for rendering Amount values Apr 29, 2026
@jessealama jessealama marked this pull request as ready for review April 29, 2026 09:38
@jessealama jessealama requested a review from eemeli April 29, 2026 09:38
Copy link
Copy Markdown
Member

@eemeli eemeli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on yesterday's call, let's incorporate here the exponential notation changes that are currently in #102, and rebase that PR on top of this with just the serialization format changes.

@jessealama jessealama changed the base branch from restring to main April 30, 2026 09:30
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread README.md Outdated
eemeli and others added 11 commits May 5, 2026 09:18
Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Adds two abstract operations to Amount:

- RenderStringInExponentialNotation: takes a String in StrDecimalLiteral
  form and rewrites it in canonical exponential form (one nonzero integer
  digit, optional fractional part, signed unpadded exponent), preserving
  the precision of the input. It uses StringIntlMV to recover the
  mathematical value and significant-digit count, then formats the
  mantissa via CreateFormatterObject + FormatNumericToString configured
  for fraction digits = digitCount - 1 (which sidesteps the 21 cap on
  the significant-digit slots).

- RenderBigIntInExponentialNotation: takes a BigInt and rewrites it in
  canonical exponential form, absorbing trailing zeros into the exponent.

Wires these through the constructor and convertTo so any String stored
in [[AmountValue]] is already in canonical exponential form, and through
toString so Number-backed values use Number.prototype.toExponential and
BigInt-backed values use the new AO. Resolves the TODO left in #102.
Replaces the explicit StrDecimalLiteral parse + StringIntlMV dance with a
single call to ToIntlMathematicalValue, which returns the same (value,
digit count) pair and also resolves the NaN/Infinity special cases
through its return value rather than by string comparison.
Co-authored-by: Eemeli Aro <eemeli@mozilla.com>
@jessealama jessealama force-pushed the flesh-out-tostring-exponential branch from 70d7e0c to 736d70c Compare May 5, 2026 07:21
jessealama added a commit that referenced this pull request May 5, 2026
The exponential-notation rendering of Amount values is the subject of
PR #104. Per Richard Gibson's suggestion, this PR is narrowed to cover
only the bracket-and-tilde serialization style. Reverts the exponential
example values, the unit-name canonicalization (kg → kilogram), and the
'canonical exponential notation' wording in toString's description.
Drops the spec algorithm's placeholder TODO line, since exponential
normalization is fleshed out in PR #104.
@jessealama jessealama requested a review from eemeli May 5, 2026 07:57
Comment thread spec.emu Outdated
@jessealama jessealama requested a review from eemeli May 5, 2026 13:38
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.

2 participants