Skip to content

fix(utils): use the right unit in the auto number formatter - #3360

Open
SwastikTripathi wants to merge 1 commit into
antvis:nextfrom
SwastikTripathi:fix/auto-formatter-units
Open

SwastikTripathi wants to merge 1 commit into
antvis:nextfrom
SwastikTripathi:fix/auto-formatter-units

Conversation

@SwastikTripathi

Copy link
Copy Markdown

👀 PR includes

🐛 Bugfix

  • Bug fix (found while reading the code, no issue)

📝 Description

auto (exported from @antv/s2) puts every number one unit too high. Its doc comment says numbers under 10,000 get thousands separators (3,459) and larger ones get a unit (3.2万, 3.4亿), but:

auto(3459);       // '3,459 万'   (should be '3,459')
auto(34590000);   // '3,459 亿'   (should be '3,459 万')
auto(1e9);        // '10 undefined' (should be '10 亿')
auto(0);          // '0 万'
auto(9999.999);   // '10,000 万'  (should be '1 万')

The while loop increments loop once more on the iteration that stops, so texts[loop - 1] is always the next unit, and once all units are used it reads past the end of texts. The existing test case pinned this output (expect(auto(3459)).toStrictEqual('3,459 万')).

The loop now counts only the units it divides by. After toFixed, a value that rounds up to the next power (9999.999 to 10000) moves to the next unit, so it becomes 1 万 instead of 10,000 万. Negative numbers are handled as before.

🖼️ Screenshot

Before After
auto(34590000) = 3,459 亿 3,459 万

⚠️ Breaking Changes

  • No

The output only changes where it had the wrong unit. Nothing inside S2 calls auto.

🔗 Related issue link

None.

🔍 Self-Check before the merge

  • Add or update test case.

Updated the cases in __tests__/unit/utils/formatter-spec.ts to the documented output and added 1e9, 3.2 万, 3.4 亿 and a rounding carry case. The new expectations fail before the change (Expected: "0", Received: "0 万") and pass after, run with jest --runner=jest-runner --env=node (the electron runner doesn't start on this machine). eslint and prettier are clean.

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.

1 participant