Skip to content

fix(dart/core): preserve large numbers in formatString - #4196

Open
leoafarias wants to merge 3 commits into
a2ui-project:mainfrom
leoafarias:fix/format-string-large-numbers
Open

leoafarias wants to merge 3 commits into
a2ui-project:mainfrom
leoafarias:fix/format-string-large-numbers

Conversation

@leoafarias

Copy link
Copy Markdown
Contributor

Description

Fixes #4193.

formatString printed whole numbers of 2^63 or more as 9223372036854775807 on the Dart VM and Wasm. _numberToString and _normalizeNumbers call toInt(), which clamps to the 64-bit limits, and _normalizeNumbers has no range check at all.

Solution

  1. Both functions call toInt() only for whole doubles in [-2^63, 2^63).
  2. Larger values keep their digits: a bare 1e20 prints 100000000000000000000. Inside a list or map the value stays a double, so jsonEncode writes [100000000000000000000.0], the right number with a .0.
  3. Updated the _normalizeNumbers doc comment and dart/a2ui_core/CHANGELOG.md.

Verification

  1. Added 36 cases to dart/a2ui_core/test/basic_catalog_test.dart: bare, list and object values around both 64-bit limits and 1e21, plus 1e300 and -1e300. 26 of them fail on main.
  2. dart test in dart/a2ui_core passes (2,398 tests), and so do dart analyze --fatal-infos and scripts/fix_format.sh --check.
  3. The same cases print identically on the VM and Wasm and correctly in a JS build, both run in Node.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

I haven't run a sample or scripts/e2e_test.sh: both need an agent backed by a live model and an API key I don't have set up here.

If you need help, consider asking for advice on the discussion board.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue in formatString where whole doubles outside the signed 64-bit range were being clamped. It updates _numberToString and _normalizeNumbers in function_support.dart to ensure that only integral doubles within the signed 64-bit range are converted to integers, while larger values remain as doubles to prevent clamping. Additionally, comprehensive tests have been added to verify this behavior across various large number boundaries and structures. I have no further feedback to provide.

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 12, 2026
@Varun-S10 Varun-S10 self-assigned this Nov 13, 2026
@github-actions github-actions Bot removed the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 13, 2026
@Varun-S10

Copy link
Copy Markdown
Collaborator

Hi @leoafarias, thank you for raising this PR and we verified that it fixes large number formatting in formatString.
Hi @gspencergoog, could you please take a look?

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 20, 2026
@polina-c

polina-c commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

From triage bot, analysis:

For humans

TL;DR
Case: this PR fixes a small Dart core bug, with tests, but has merge conflicts with main.
Suggestion: ask the author to resolve the conflicts, assign gspencergoog and add the type and component labels.

Summary:
The PR stops formatString in the Dart a2ui_core from printing whole numbers of 2^63 or more as 9223372036854775807. It fixes #4193 and adds 36 test cases.

Targeted issues:
#4193

Suggestion:
Add status: waiting-for-author-response and ask the author to merge main and resolve the conflicts: GitHub reports that the PR conflicts with main. Since the PR was opened, #4305 reformatted function_support.dart, and #4258 prepared the Dart 0.8.0 release in the same CHANGELOG.md. Also assign gspencergoog to review it, and add type: bug and component: core sdk. Varun-S10 assigned themselves, verified the fix and asked gspencergoog to take a look, but no team member has reviewed the PR yet. gspencergoog wrote 6 of the 10 commits to function_support.dart.

The PR meets the review criteria:

  1. It is a small bug fix with tests. The bug is still on main: _numberToString and _normalizeNumbers call toInt(), which clamps to the 64-bit limits, and _normalizeNumbers has no range check.
  2. It does not overlap with other work. #4114 (a draft) and #2541 also change function_support.dart, but not these two functions.
  3. The team can maintain it: the change is a range check in two functions, plus tests.

#4230, by the same author, asks to export the conversion that formatString uses, and names this PR as a fix that a copy of the conversion could miss.

  1. Add labels: status: waiting-for-author-response, type: bug, component: core sdk
  2. Assign: gspencergoog
  3. Related items: [FEATURE]: Export coerceToString from a2ui_core #4230

Reply:

Thanks for the fix. This PR now has merge conflicts with main. Could you merge the latest main and resolve them? We will review the PR once the conflicts are resolved.

To adjust the analysis and apply its suggestion, run in your a2ui checkout:

tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4196
For software
<a2ui>
root = TriageAnalysis(
  tldr="""Case: this PR fixes a small Dart core bug, with tests, but has merge conflicts with `main`.
Suggestion: ask the author to resolve the conflicts, assign `gspencergoog` and add the type and component labels.""",
  summary="The PR stops `formatString` in the Dart `a2ui_core` from printing whole numbers of 2^63 or more as `9223372036854775807`. It fixes [#4193](https://github.com/a2ui-project/a2ui/issues/4193) and adds 36 test cases.",
  suggestion="""Add `status: waiting-for-author-response` and ask the author to merge `main` and resolve the conflicts: GitHub reports that the PR conflicts with `main`. Since the PR was opened, [#4305](https://github.com/a2ui-project/a2ui/pull/4305) reformatted `function_support.dart`, and [#4258](https://github.com/a2ui-project/a2ui/pull/4258) prepared the Dart 0.8.0 release in the same `CHANGELOG.md`. Also assign `gspencergoog` to review it, and add `type: bug` and `component: core sdk`. `Varun-S10` assigned themselves, [verified](https://github.com/a2ui-project/a2ui/pull/4196#issuecomment-6847140250) the fix and asked `gspencergoog` to take a look, but no team member has reviewed the PR yet. `gspencergoog` wrote [6 of the 10 commits](https://github.com/a2ui-project/a2ui/commits/main/dart/a2ui_core/lib/src/basic_catalog/function_support.dart) to `function_support.dart`.

The PR meets the review criteria:

1. It is a small bug fix with tests. The bug is still on `main`: [`_numberToString` and `_normalizeNumbers`](https://github.com/a2ui-project/a2ui/blob/172cf31e1acbce31c4e60d82b2edbd923b279dd8/dart/a2ui_core/lib/src/basic_catalog/function_support.dart#L112-L131) call `toInt()`, which clamps to the 64-bit limits, and `_normalizeNumbers` has no range check.
2. It does not overlap with other work. [#4114](https://github.com/a2ui-project/a2ui/pull/4114) (a draft) and [#2541](https://github.com/a2ui-project/a2ui/pull/2541) also change `function_support.dart`, but not these two functions.
3. The team can maintain it: the change is a range check in two functions, plus tests.

[#4230](https://github.com/a2ui-project/a2ui/issues/4230), by the same author, asks to export the conversion that `formatString` uses, and names this PR as a fix that a copy of the conversion could miss.""",
  reply="Thanks for the fix. This PR now has merge conflicts with `main`. Could you merge the latest `main` and resolve them? We will review the PR once the conflicts are resolved.",
  added_labels=["status: waiting-for-author-response", "type: bug", "component: core sdk"],
  new_assignees=["gspencergoog"],
  targeted_issues=[4193],
  related_items=[4230]
)
</a2ui>

This analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision.

🤖 Agent-generated

@polina-c

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the tests. This PR now has merge conflicts with main in function_support.dart and CHANGELOG.md, after #4305 reformatted the Dart packages. Could you merge the latest main and resolve them? We will review the PR after that.

@polina-c polina-c added component: core sdk Affects the core SDK layer in any language status: waiting-for-author-response type: bug Something isn't working and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Nov 20, 2026
Resolve the function_support.dart conflict with a2ui-project#4305's reformat and
move the CHANGELOG entry under Unreleased, since 0.8.0 has shipped.
Move the CHANGELOG entry under a new Unreleased section, since 0.9.0
has shipped.
@leoafarias

Copy link
Copy Markdown
Contributor Author

Thanks @polina-c! Merged main and fixed the conflicts. I moved the changelog entry under Unreleased since 0.9.0 already shipped. Tests pass, ready for another look.

pinieb added a commit that referenced this pull request Dec 3, 2026
…ke JavaScript (#4362)

## Problem

The Swift SDK turns a number into text with `Double.description` in two
places: `formatString` interpolation, and `NodeResolver` when it coerces
a `DynamicString` bound to a number (for example a `Text` whose `text`
is `{"path": "/amount"}`). Swift writes very small and very large
numbers with a zero-padded exponent, so the same data renders
differently than in the web renderers, which use JavaScript's
`String(number)`:

| Value | Swift before | Web (`String(n)`) |
|---|---|---|
| `0.00001` | `1e-05` | `0.00001` |
| `1e-7` | `1e-07` | `1e-7` |
| `1e20` | `1e+20` | `100000000000000000000` |
| `2^63` | `9.223372036854776e+18` | `9223372036854776000` |

The existing `Int(exactly:)` branch only covers integers below 2^63, so
`1e19` and larger also get an exponent. #4196 fixes the large-integer
case in Dart.

## Changes

1. Add a package-level `Double.ecmaScriptString` in `A2UICore`. It takes
the shortest round-trip digits from `description` and lays them out by
the ECMAScript `Number::toString` rules: no exponent for magnitudes from
`1e-7` up to `1e21`, otherwise `1e+21` or `1.5e-7` style. `-0` is
written as `0`, and NaN and the infinities as `NaN`, `Infinity` and
`-Infinity`.
2. `FormatStringFunction` and `NodeResolver.coerceToString` use it for
`.number` values. `.integer` values and the JSON output for objects and
arrays don't change.
3. Tests: a parameterized `DoubleECMAScriptStringTests` (expected
strings taken from Node.js, each value also checked negated), a
`formatString` interpolation test, and a `NodeResolver` test for a
`Text` bound to a number.
4. A `swift/CHANGELOG.md` entry.

I also compared the helper against Node's `String(number)` for 200,000
doubles (random bit patterns, random values in several ranges, and
decimal values with exponents from -12 to 24) and found no differences.
That script is not part of the PR.

The core conformance suite (`conformance/core/functions.yaml`) has no
case for this, and I didn't add one: the Python core writes `1e-05` (it
uses `str(float)`), and Dart needs #4196 first. Shared cases can follow
once all SDKs agree.

## Pre-launch Checklist

One time:

- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I read the [Style Guide].
- [ ] I have built and run at least one [sample].

For this PR:

- [x] I have updated the relevant CHANGELOG.md file.
- [ ] I updated/added relevant documentation.
- [x] My code changes (if any) have tests.
- [ ] If my branch is on a fork, I have verified that
[scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh)
passes.

If you need help, consider asking for advice on the [discussion board].

<!-- Links -->

[CLA]: https://cla.developers.google.com/
[Contributors Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md
[discussion board]: https://github.com/a2ui-project/a2ui/discussions
[Style Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style
[sample]:
https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples

---------

Signed-off-by: Pete Biencourt <piebie@google.com>
Co-authored-by: Pete Biencourt <piebie@google.com>

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

component: core sdk Affects the core SDK layer in any language status: needs review type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: a2ui_core's formatString prints numbers of 2^63 or more as 9223372036854775807

3 participants