Skip to content

Fix locale-sensitive decimal parsing in compiled bash/powershell scripts - #6

Open
StacheSebastian wants to merge 1 commit into
tronschell:mainfrom
StacheSebastian:fix/locale-sensitive-parsing
Open

StacheSebastian wants to merge 1 commit into
tronschell:mainfrom
StacheSebastian:fix/locale-sensitive-parsing

Conversation

@StacheSebastian

Copy link
Copy Markdown

Summary

Both compiler backends parse numeric fields from the statusline JSON (cost,
percentages, timestamps) using APIs that are sensitive to the host's locale.
On any system where , rather than . is the decimal separator (the
majority of non-English locales — German, French, Spanish, Polish, Russian,
Dutch, Nordic languages, Portuguese, Turkish, etc.), this silently corrupts
the rendered statusline instead of erroring:

  • Cost"total_cost_usd": 6.125108349213767 renders as
    $6125108000000000,00 on PowerShell, or fails with printf: invalid number and truncates to $6,00 on bash.
  • Rate-limit bars — a used percentage of 2.34 renders as a fully
    filled
    progress bar (parsed as 234, then clamped to 100).

Root cause

  • PowerShell (__costFmt / __bar / __relTime): [double]::TryParse($v, [ref]$n)
    — the 2-arg overload — defaults to NumberStyles.Float | NumberStyles.AllowThousands
    with CultureInfo.CurrentCulture. Under a comma-decimal culture, . is
    the culture's group separator, so AllowThousands silently strips every
    . instead of treating it as a decimal point, concatenating the digits
    into a huge integer.
    (The [double](...) cast used for the gt/lt conditional emitters is
    not affected by this — verified separately — so that code path is
    untouched.)
  • Bash (__cost_fmt, and the gt/lt awk conditionals): printf '$%.*f' and awk's string-to-number coercion can honor LC_NUMERIC/
    LC_ALL, so a .-decimal JSON value fails to parse correctly under a
    comma-decimal locale.

Repro

$n = 0.0
[double]::TryParse("6.125108349213767", [ref]$n)  # under a de-DE culture
# $n → 6125108349213770 instead of 6.125108349213767

LC_ALL=de_DE.UTF-8 bash -c 'printf "\$%.2f\n" "6.125108349213767"'
# bash: printf: 6.125108349213767: invalid number
# $6,00   (cents silently dropped)

Fix

Force invariant/C numeric parsing at every point a JSON-sourced decimal
string is converted to a number, regardless of the host's locale:

  • PowerShell: added a __toDouble helper using
    [double]::TryParse($v, [NumberStyles]::Float, [CultureInfo]::InvariantCulture, [ref]$n),
    and routed __costFmt, __bar, and __relTime through it.
  • Bash: prefixed the __cost_fmt printf call and the gt/lt awk
    conditional checks with LC_ALL=C.

@vercel

vercel Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

@StacheSebastian is attempting to deploy a commit to the tronschell's projects Team on Vercel.

A member of the Team first needs to authorize it.

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