From a2962814081d69d15b15d5273f4c3402b891a47e Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:54:59 +0300 Subject: [PATCH] fix(mathutil): format TruncRound integer inputs as floats (#365) --- mathutil/mathutil.go | 2 +- mathutil/mathutil_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 035fa6e..3a03dbe 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -93,7 +93,7 @@ func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 // TruncRound round off n decimal places. // Play: https://go.dev/play/p/aumarSHIGzP func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T { - floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x) + floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", float64(x)) temp := strings.Split(floatStr, ".") var newFloat string if len(temp) < 2 || n >= len(temp[1]) { diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index c304306..1a5acad 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -17,6 +17,12 @@ func TestExponent(t *testing.T) { assert.Equal(int64(100), Exponent(10, 2)) } +func TestTruncRoundInteger(t *testing.T) { + assert := internal.NewAssert(t, "TestTruncRoundInteger") + assert.Equal(42, TruncRound(42, 0)) + assert.Equal(int8(-42), TruncRound(int8(-42), 2)) +} + func TestLCMIntermediateOverflow(t *testing.T) { assert := internal.NewAssert(t, "TestLCMIntermediateOverflow") assert.Equal(uint8(32), LCM(uint8(32)))