124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
// Package utils 提供通用工具函数
|
||
// 包括数据类型转换、时间处理、网络工具等
|
||
package utils
|
||
|
||
import (
|
||
"math"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
// String2Int 字符串转Int
|
||
// intStr: 数字的字符串
|
||
// 返回: 转换后的整数,转换失败返回0
|
||
func String2Int(intStr string) (intNum int) {
|
||
intNum, err := strconv.Atoi(intStr)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return
|
||
}
|
||
|
||
// String2Int64 字符串转Int64
|
||
// intStr: 数字的字符串
|
||
// 返回: 转换后的64位整数,转换失败返回0
|
||
func String2Int64(intStr string) (int64Num int64) {
|
||
intNum, err := strconv.ParseInt(intStr, 10, 64)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return intNum
|
||
}
|
||
|
||
// String2Float64 字符串转Float64
|
||
// floatStr: 小数点数字的字符串
|
||
// 返回: 转换后的64位浮点数,转换失败返回0
|
||
func String2Float64(floatStr string) (floatNum float64) {
|
||
floatNum, err := strconv.ParseFloat(floatStr, 64)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return
|
||
}
|
||
|
||
// String2Float32 字符串转Float32
|
||
// floatStr: 小数点数字的字符串
|
||
// 返回: 转换后的32位浮点数,转换失败返回0
|
||
func String2Float32(floatStr string) (floatNum float32) {
|
||
floatNum64, err := strconv.ParseFloat(floatStr, 32)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
floatNum = float32(floatNum64)
|
||
return
|
||
}
|
||
|
||
// Int2String Int转字符串
|
||
// intNum: 整数
|
||
// 返回: 转换后的字符串
|
||
func Int2String(intNum int) (intStr string) {
|
||
intStr = strconv.Itoa(intNum)
|
||
return
|
||
}
|
||
|
||
// Int642String Int64转字符串
|
||
// intNum: 64位整数
|
||
// 返回: 转换后的字符串
|
||
func Int642String(intNum int64) (int64Str string) {
|
||
// 10代表10进制
|
||
int64Str = strconv.FormatInt(intNum, 10)
|
||
return
|
||
}
|
||
|
||
// Float64ToString Float64转字符串
|
||
// floatNum: float64数字
|
||
// prec: 精度位数(不传则默认float数字精度)
|
||
// 返回: 转换后的字符串
|
||
func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
|
||
if len(prec) > 0 {
|
||
floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
|
||
return
|
||
}
|
||
floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
|
||
return
|
||
}
|
||
|
||
// Float32ToString Float32转字符串
|
||
// floatNum: float32数字
|
||
// prec: 精度位数(不传则默认float数字精度)
|
||
// 返回: 转换后的字符串
|
||
func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
|
||
if len(prec) > 0 {
|
||
floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
|
||
return
|
||
}
|
||
floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
|
||
return
|
||
}
|
||
|
||
// BinaryToDecimal 二进制转10进制
|
||
// bit: 二进制字符串
|
||
// 返回: 转换后的十进制数
|
||
func BinaryToDecimal(bit string) (num int) {
|
||
fields := strings.Split(bit, "")
|
||
lens := len(fields)
|
||
var tempF float64 = 0
|
||
for i := 0; i < lens; i++ {
|
||
floatNum := String2Float64(fields[i])
|
||
tempF += floatNum * math.Pow(2, float64(lens-i-1))
|
||
}
|
||
num = int(tempF)
|
||
return
|
||
}
|
||
|
||
// AnyToString 任意类型转字符串
|
||
// in: 输入值
|
||
// 返回: 转换后的字符串
|
||
func AnyToString(in any) (s string) {
|
||
if in == nil {
|
||
return ""
|
||
}
|
||
|
||
return in.(string)
|
||
}
|