2025-10-03 19:55:20 +08:00
|
|
|
|
// Package utils 提供通用工具函数
|
|
|
|
|
// 包括数据类型转换、时间处理、网络工具等
|
2025-02-07 13:01:38 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-22 19:02:38 +08:00
|
|
|
|
// String2Int 字符串转Int
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// intStr: 数字的字符串
|
|
|
|
|
// 返回: 转换后的整数,转换失败返回0
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func String2Int(intStr string) (intNum int) {
|
2025-09-22 19:02:38 +08:00
|
|
|
|
intNum, err := strconv.Atoi(intStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2025-02-07 13:01:38 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 19:02:38 +08:00
|
|
|
|
// String2Int64 字符串转Int64
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// intStr: 数字的字符串
|
|
|
|
|
// 返回: 转换后的64位整数,转换失败返回0
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func String2Int64(intStr string) (int64Num int64) {
|
2025-10-03 19:55:20 +08:00
|
|
|
|
intNum, err := strconv.ParseInt(intStr, 10, 64)
|
2025-09-22 19:02:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2025-10-03 19:55:20 +08:00
|
|
|
|
return intNum
|
2025-02-07 13:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 19:02:38 +08:00
|
|
|
|
// String2Float64 字符串转Float64
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// floatStr: 小数点数字的字符串
|
|
|
|
|
// 返回: 转换后的64位浮点数,转换失败返回0
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func String2Float64(floatStr string) (floatNum float64) {
|
2025-09-22 19:02:38 +08:00
|
|
|
|
floatNum, err := strconv.ParseFloat(floatStr, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2025-02-07 13:01:38 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 19:02:38 +08:00
|
|
|
|
// String2Float32 字符串转Float32
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// floatStr: 小数点数字的字符串
|
|
|
|
|
// 返回: 转换后的32位浮点数,转换失败返回0
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func String2Float32(floatStr string) (floatNum float32) {
|
2025-09-22 19:02:38 +08:00
|
|
|
|
floatNum64, err := strconv.ParseFloat(floatStr, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2025-02-07 13:01:38 +08:00
|
|
|
|
floatNum = float32(floatNum64)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// Int2String Int转字符串
|
|
|
|
|
// intNum: 整数
|
|
|
|
|
// 返回: 转换后的字符串
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func Int2String(intNum int) (intStr string) {
|
|
|
|
|
intStr = strconv.Itoa(intNum)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// Int642String Int64转字符串
|
|
|
|
|
// intNum: 64位整数
|
|
|
|
|
// 返回: 转换后的字符串
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func Int642String(intNum int64) (int64Str string) {
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// 10代表10进制
|
2025-02-07 13:01:38 +08:00
|
|
|
|
int64Str = strconv.FormatInt(intNum, 10)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// Float64ToString Float64转字符串
|
|
|
|
|
// floatNum: float64数字
|
|
|
|
|
// prec: 精度位数(不传则默认float数字精度)
|
|
|
|
|
// 返回: 转换后的字符串
|
2025-02-07 13:01:38 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// Float32ToString Float32转字符串
|
|
|
|
|
// floatNum: float32数字
|
|
|
|
|
// prec: 精度位数(不传则默认float数字精度)
|
|
|
|
|
// 返回: 转换后的字符串
|
2025-02-07 13:01:38 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// BinaryToDecimal 二进制转10进制
|
|
|
|
|
// bit: 二进制字符串
|
|
|
|
|
// 返回: 转换后的十进制数
|
2025-02-07 13:01:38 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 19:55:20 +08:00
|
|
|
|
// AnyToString 任意类型转字符串
|
|
|
|
|
// in: 输入值
|
|
|
|
|
// 返回: 转换后的字符串
|
2025-02-07 13:01:38 +08:00
|
|
|
|
func AnyToString(in any) (s string) {
|
|
|
|
|
if in == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return in.(string)
|
|
|
|
|
}
|