From ffa608147106019371f5bbb77d9740ad3c55e1ee Mon Sep 17 00:00:00 2001 From: UncleChair Date: Wed, 13 Nov 2024 23:25:24 +0800 Subject: [PATCH] fix(util/gconv): fix missing minus in string to int64 conversion (#3920) --- util/gconv/gconv_int.go | 3 +++ util/gconv/gconv_z_unit_int_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/util/gconv/gconv_int.go b/util/gconv/gconv_int.go index 6ba101d2815..8b389f754ed 100644 --- a/util/gconv/gconv_int.go +++ b/util/gconv/gconv_int.go @@ -125,6 +125,9 @@ func Int64(any interface{}) int64 { if valueInt64 := Float64(s); math.IsNaN(valueInt64) { return 0 } else { + if isMinus { + return -int64(valueInt64) + } return int64(valueInt64) } default: diff --git a/util/gconv/gconv_z_unit_int_test.go b/util/gconv/gconv_z_unit_int_test.go index bf2dda6bc34..9129896f39a 100644 --- a/util/gconv/gconv_z_unit_int_test.go +++ b/util/gconv/gconv_z_unit_int_test.go @@ -69,6 +69,7 @@ var intTests = []struct { {"0XA", 10, 10, 10, 10, 10}, {"-0XA", -10, -10, -10, -10, -10}, {"123.456", 123, 123, 123, 123, 123}, + {"-123.456", -123, -123, -123, -123, -123}, {"true", 0, 0, 0, 0, 0}, {"false", 0, 0, 0, 0, 0}, {"on", 0, 0, 0, 0, 0},