Issue #878 - Render long amounts in the browser locale, so they localize like short ones

Closes #878 (closed).

Problem

formatNumber (lib/utils.ts) formats amounts of up to 15 characters with toLocaleString (browser locale), but longer ones, such as most witness vote totals in raw VESTS, by hand with , and .. Callers pass the result to relocalizeAmount, which reads it in the browser's locale to restate it in the app's language. In a decimal-comma browser it couldn't read the hand-built form and returned it unchanged.

Reproduced on hivescan.info with a German browser and the site in German, /witnesses in VESTS (first row):

Votes Today 7d
Before (production) 161,302,494,029.166509 +87.753.722,728876 -4,838,055,429.259156
After (this branch) 161.302.557.781,143869 +87.812.792,021232 -4.838.109.055,890387

Change

  • utils/localeAmount.ts: the exact rendering inside relocalizeAmount (integer part through BigInt, fraction digit for digit, locale digits and minus sign) is extracted as renderExactDecimal(integer, fraction, locale?). relocalizeAmount calls it, so its behaviour is unchanged (its 9 tests pass as before).
  • lib/utils.ts: formatNumber's long branch renders through renderExactDecimal in the browser locale, like the short branch. No digits are lost. Inputs that aren't plain integer strings keep the old output instead of throwing.

The skipPrecision branch also writes , and . by hand, but its callers (RC delegations, proposal budgets, the impact simulator) don't pass the result to relocalizeAmount, so they don't mix formats; left as is.

Checked

  • Dev server against testapi, with all languages on, in VESTS mode. The first row's three amounts now match in every combination:

    • German app with a German or an English browser: 161.302.557.781,143869.
    • French app with a German browser: 161 302 587 608,760286.
    • English app with an English browser: unchanged, 161,302,591,098.713720.
  • New tests/unit/formatNumberLocale.test.ts (6 tests). It simulates German, Swedish, Finnish, Arabic and English browsers:

    • long and short amounts come out in the same format;
    • relocalizeAmount reads the long ones in a German browser;
    • every digit is kept, also with Arabic-Indic digits;
    • nothing changes in an English browser;
    • negatives with a locale's own minus sign are read back;
    • malformed input keeps its old output.

    4 of the 6 fail on develop.

  • tsc --noEmit clean, Jest 764/764.

CSV exports: in a non-English browser, exported long amounts (e.g. the Voters and Votes History dialogs) now use the browser's separators, as short amounts already did. A locale-neutral export would be a separate change covering both.

Review round (one independent reviewer)

No High or Medium. Checked and holding:

  • relocalizeAmount's output is unchanged by the extraction.
  • Long and short output match in de-CH, sv, fi, en-IN, fr, es, ar-EG and Devanagari digits: grouping, decimal mark, digits and minus.
  • relocalizeAmount reads the new output back.
  • Nothing parses formatNumber output on an affected path.
  • The e2e tests are unaffected.
  • No new server-rendering risk: the amounts load client-side.
# Severity Finding Status
1 Low (existing) In sv / fi / nb browsers (U+2212) and ar-EG (U+061C before the minus), relocalizeAmount couldn't read negatives and left them in browser format Fixed in 3d1d61c0, with a test (fails without the fix)
2 Low CSV exports of long amounts now use the browser's separators, like short ones already did Noted above
3 Low The test's stand-in Intl.NumberFormat lacked the real one's static members Fixed
Edited by mcfarhat

Merge request reports

Loading
Loading