1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FloatUtil.go 718 Bytes
一键复制 编辑 原始数据 按行查看 历史
laowei 提交于 2024-07-15 14:55 +08:00 . bricks2准备
package data
import (
"fmt"
"strconv"
"unsafe"
)
type FloatUtil struct{}
// 对象(字符串)转浮点数
func (fu FloatUtil) ToStr(obj float64) string {
return fmt.Sprintf("%f", obj)
}
// 对象(字符串)转64整型
func (fu FloatUtil) ToFloat(obj interface{}, bitSize int, iDefault float64) float64 {
var str string
switch obj.(type) {
case []uint8:
str = fu.Byte2Str(obj.([]uint8))
default:
str = fmt.Sprintf("%v", obj)
}
if str == "" { //字符串不能判断nil
return iDefault
}
result, err := strconv.ParseFloat(str, bitSize)
if err != nil {
return iDefault
}
return result
}
// Byte转Str
func (fu FloatUtil) Byte2Str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
fed9db1a323d

搜索帮助