加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
convert.go 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
张国凯 提交于 2016-10-31 00:01 . -Cinit
package utilGo
import "strconv"
//String 将传入的类型转为字符串
func String(val interface{}) string {
switch val.(type) {
case string:
return val.(string)
case int:
return strconv.FormatInt(int64(val.(int)), 10)
case int8:
return strconv.FormatInt(int64(val.(int8)), 10)
case int16:
return strconv.FormatInt(int64(val.(int16)), 10)
case int32:
return strconv.FormatInt(int64(val.(int32)), 10)
case int64:
return strconv.FormatInt(int64(val.(int64)), 10)
case uint:
return strconv.FormatUint(uint64(val.(uint)), 10)
case uint8:
return strconv.FormatUint(uint64(val.(uint8)), 10)
case uint16:
return strconv.FormatUint(uint64(val.(uint16)), 10)
case uint32:
return strconv.FormatUint(uint64(val.(uint32)), 10)
case uint64:
return strconv.FormatUint(uint64(val.(uint64)), 10)
case bool:
return strconv.FormatBool(val.(bool))
case float32:
return strconv.FormatFloat(val.(float64), 'f', -1, 32)
case float64:
return strconv.FormatFloat(val.(float64), 'f', -1, 64)
default:
return ""
}
}
//FormatFloat 浮点数转String 保留指定位数的小数
func FormatFloat(v interface{}, decimal int) string {
switch v.(type) {
case float32:
return strconv.FormatFloat(float64(v.(float32)), 'f', decimal, 32)
case float64:
return strconv.FormatFloat(float64(v.(float64)), 'f', decimal, 64)
default:
return ""
}
}
//FormatInt 整数转字符串
func FormatInt(val interface{}) string {
switch val.(type) {
case int:
return strconv.FormatInt(int64(val.(int)), 10)
case int8:
return strconv.FormatInt(int64(val.(int8)), 10)
case int16:
return strconv.FormatInt(int64(val.(int16)), 10)
case int32:
return strconv.FormatInt(int64(val.(int32)), 10)
case int64:
return strconv.FormatInt(int64(val.(int64)), 10)
case uint:
return strconv.FormatUint(uint64(val.(uint)), 10)
case uint8:
return strconv.FormatUint(uint64(val.(uint8)), 10)
case uint16:
return strconv.FormatUint(uint64(val.(uint16)), 10)
case uint32:
return strconv.FormatUint(uint64(val.(uint32)), 10)
case uint64:
return strconv.FormatUint(uint64(val.(uint64)), 10)
default:
return ""
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化