Skip to content

fix: support negative precision in Truncate#419

Open
Yanhu007 wants to merge 1 commit intoshopspring:masterfrom
Yanhu007:fix/truncate-negative-precision
Open

fix: support negative precision in Truncate#419
Yanhu007 wants to merge 1 commit intoshopspring:masterfrom
Yanhu007:fix/truncate-negative-precision

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #406

Problem

Truncate ignores negative precision values due to a precision >= 0 guard:

d := decimal.RequireFromString("5432")
d.Truncate(-2).String() // returns "5432" (unchanged)
// expected: "5400"

Fix

Remove the precision >= 0 check. The existing rescale logic already handles negative precision correctly — it truncates integer digits.

decimal.RequireFromString("5432").Truncate(-2)  // "5400" ✅
decimal.RequireFromString("5432").Truncate(-1)  // "5430" ✅
decimal.RequireFromString("123.456").Truncate(2) // "123.45" ✅ (unchanged)

Testing

All existing tests pass. The fix is a single-line change (removing precision >= 0 &&).

Truncate ignored negative precision values due to a precision >= 0
guard. Negative precision is useful for truncating integer digits,
e.g., Truncate(-2) rounds 5432 to 5400.

Remove the guard and update documentation with examples.

Fixes shopspring#406
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.

Truncate should parse negative values too, or return error

1 participant