From 7c77aa00fb06ffe313000f7c8c28323554ac9fad Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:56:14 +0300 Subject: [PATCH] fix(formatter): exclude leading plus signs from comma groups (#382) --- formatter/formatter.go | 11 ++++------- formatter/formatter_test.go | 3 +++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/formatter/formatter.go b/formatter/formatter.go index 9b08655..f166919 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -26,8 +26,9 @@ func Comma[T constraints.Float | constraints.Integer | string](value T, prefixSy return "" } - isNegative := strings.HasPrefix(numString, "-") - if isNegative { + sign := "" + if strings.HasPrefix(numString, "-") || strings.HasPrefix(numString, "+") { + sign = numString[:1] numString = numString[1:] } @@ -41,11 +42,7 @@ func Comma[T constraints.Float | constraints.Integer | string](value T, prefixSy numString = numString[:index] + "," + numString[index:] } - if isNegative { - numString = "-" + numString - } - - return prefixSymbol + numString + return prefixSymbol + sign + numString } // Pretty data to JSON string. diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 7620934..06ea417 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -11,6 +11,9 @@ func TestComma(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestComma") + assert.Equal("+123", Comma("+123", "")) + assert.Equal("+123,456", Comma("+123456", "")) + assert.Equal("$+123,456.78", Comma("+123456.78", "$")) assert.Equal("", Comma("", "")) assert.Equal("", Comma("aa", ""))