From 0ae7f92b0db92cf583756e752b4906e16d2a9cf9 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Tue, 14 Apr 2026 14:10:13 +0800 Subject: [PATCH] fix: support negative precision in Truncate 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 #406 --- decimal.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/decimal.go b/decimal.go index 880d569..3ff4366 100644 --- a/decimal.go +++ b/decimal.go @@ -1753,13 +1753,16 @@ func (d Decimal) Ceil() Decimal { // Truncate truncates off digits from the number, without rounding. // -// NOTE: precision is the last digit that will not be truncated (must be >= 0). +// NOTE: precision is the last digit that will not be truncated. +// Positive values truncate decimal places; negative values truncate +// integer digits (e.g., -2 rounds to nearest hundred). // // Example: // // decimal.NewFromString("123.456").Truncate(2).String() // "123.45" +// decimal.NewFromString("5432").Truncate(-2).String() // "5400" func (d Decimal) Truncate(precision int32) Decimal { - if precision >= 0 && -precision > d.exp { + if -precision > d.exp { return d.rescale(-precision) } return d