Skip to content

fix: keep the decimal point when rounding gains a digit - #200

Open
SashaMIT wants to merge 1 commit into
ericlagergren:masterfrom
SashaMIT:fix/format-round-carry
Open

SashaMIT wants to merge 1 commit into
ericlagergren:masterfrom
SashaMIT:fix/format-round-carry

Conversation

@SashaMIT

@SashaMIT SashaMIT commented Oct 9, 2026 •

Copy link
Copy Markdown

Summary

fmt.Sprintf("%.2f", d) for 9.996208266660 printed 1.000 instead of 10.00. The same path printed 0.099999999999999999999999999999998 with %.6f as 0.010000 instead of 0.100000.

When rounding carries from 999 to 1000, roundString keeps the extra digit and leaves the caller to place the point. The formatter treated that digit as a discarded place, so the point moved one position left and the displayed value was ten times too small. Values below 1 then dropped that carry digit as well.

9.94 with %.1f is still 9.9. 1.25 with %.1f is still 1.2. 2.5 with %.0f is still 2.

Test plan

  • TestDecimal_Format fails on the old formatter (1.000 vs 10.00) and passes with this change
  • go test -count=1 -run 'TestDecimal_Format|TestRoundString|TestBig_Format|TestDecomposer'

Fixes #178
Fixes #198

Formatting 9.996 with %.2f printed 1.000. A carry from 999 to 1000 was counted as a discarded place, so the point moved left by one.

Fixes ericlagergren#178
Fixes ericlagergren#198
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.

Strange rounding issue Incorrect string formatting when rounding up to include additional digit

1 participant