mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 09:42:28 +08:00
fix: fix bug of issue#112 (ToString precision lost when conver float32 and float64)
This commit is contained in:
@@ -82,33 +82,45 @@ func ToChar(s string) []string {
|
|||||||
|
|
||||||
// ToString convert value to string
|
// ToString convert value to string
|
||||||
func ToString(value interface{}) string {
|
func ToString(value interface{}) string {
|
||||||
res := ""
|
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return res
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.ValueOf(value)
|
switch val := value.(type) {
|
||||||
|
case float32:
|
||||||
switch value.(type) {
|
return strconv.FormatFloat(float64(val), 'f', -1, 32)
|
||||||
case float32, float64:
|
case float64:
|
||||||
res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
return strconv.FormatFloat(val, 'f', -1, 64)
|
||||||
return res
|
case int:
|
||||||
case int, int8, int16, int32, int64:
|
return strconv.FormatInt(int64(val), 10)
|
||||||
res = strconv.FormatInt(v.Int(), 10)
|
case int8:
|
||||||
return res
|
return strconv.FormatInt(int64(val), 10)
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case int16:
|
||||||
res = strconv.FormatUint(v.Uint(), 10)
|
return strconv.FormatInt(int64(val), 10)
|
||||||
return res
|
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:
|
case string:
|
||||||
res = v.String()
|
return val
|
||||||
return res
|
|
||||||
case []byte:
|
case []byte:
|
||||||
res = string(v.Bytes())
|
return string(val)
|
||||||
return res
|
|
||||||
default:
|
default:
|
||||||
newValue, _ := json.Marshal(value)
|
b, err := json.Marshal(val)
|
||||||
res = string(newValue)
|
if err != nil {
|
||||||
return res
|
return ""
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user