From ad9fd196ce2ff0d9dc7045046e6ff69a011d40a9 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 15 Jun 2023 14:40:30 +0800 Subject: [PATCH] fix: fix bug of issue#112 (ToString precision lost when conver float32 and float64) --- convertor/convertor.go | 54 ++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/convertor/convertor.go b/convertor/convertor.go index 6af5642..0522886 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -82,33 +82,45 @@ func ToChar(s string) []string { // ToString convert value to string func ToString(value interface{}) string { - res := "" if value == nil { - return res + return "" } - v := reflect.ValueOf(value) - - switch value.(type) { - case float32, float64: - res = strconv.FormatFloat(v.Float(), 'f', -1, 64) - return res - case int, int8, int16, int32, int64: - res = strconv.FormatInt(v.Int(), 10) - return res - case uint, uint8, uint16, uint32, uint64: - res = strconv.FormatUint(v.Uint(), 10) - return res + switch val := value.(type) { + case float32: + return strconv.FormatFloat(float64(val), 'f', -1, 32) + case float64: + return strconv.FormatFloat(val, 'f', -1, 64) + case int: + return strconv.FormatInt(int64(val), 10) + case int8: + return strconv.FormatInt(int64(val), 10) + case int16: + return strconv.FormatInt(int64(val), 10) + case int32: + return strconv.FormatInt(int64(val), 10) + case int64: + return strconv.FormatInt(val, 10) + case uint: + return strconv.FormatUint(uint64(val), 10) + case uint8: + return strconv.FormatUint(uint64(val), 10) + case uint16: + return strconv.FormatUint(uint64(val), 10) + case uint32: + return strconv.FormatUint(uint64(val), 10) + case uint64: + return strconv.FormatUint(val, 10) case string: - res = v.String() - return res + return val case []byte: - res = string(v.Bytes()) - return res + return string(val) default: - newValue, _ := json.Marshal(value) - res = string(newValue) - return res + b, err := json.Marshal(val) + if err != nil { + return "" + } + return string(b) } }