package utility import ( "bytes" "encoding/json" "fmt" "github.com/shopspring/decimal" "github.com/sirupsen/logrus" "github.com/spf13/cast" "math" "time" ) func FormatInt64ToTimeStr(dateNum int64) string { stamp := time.Unix(dateNum, 0) return stamp.Format("2006-01-02") } func FormatToBytes(data interface{}) []byte { content, _ := json.Marshal(data) return content } // IsEven 判断是偶数 func IsEven(num int) bool { if num%2 == 0 { return true } return false } // Round 四舍五入保留小数,默认2位 func Round(value float64, args ...interface{}) (v string) { var places int32 = 2 if len(args) > 0 { places = cast.ToInt32(args[0]) } return decimal.NewFromFloat(value).Round(places).String() } // RoundToFloat 四舍五入保留小数,默认2位 func RoundToFloat(value float64, args ...interface{}) (v float64) { var places int32 = 2 if len(args) > 0 { places = cast.ToInt32(args[0]) } v, _ = decimal.NewFromFloat(value).Round(places).Float64() return } func JSONMethod(content interface{}) map[string]interface{} { var name map[string]interface{} if marshalContent, err := json.Marshal(content); err != nil { logrus.Warnf("JSONMethod Marshal err:%v", err) } else { d := json.NewDecoder(bytes.NewReader(marshalContent)) d.UseNumber() // 设置将float64转为一个number if err := d.Decode(&name); err != nil { fmt.Println(err) } else { for k, v := range name { name[k] = v } } } return name } func Rmu0000(s string) string { str := make([]rune, 0, len(s)) for _, v := range []rune(s) { if v == 0 { continue } str = append(str, v) } return string(str) } // UniqueInt64s 切片去重 func UniqueInt64s(s []int64) []int64 { m := make(map[int64]struct{}, 0) newS := make([]int64, 0) for _, i2 := range s { if _, ok := m[i2]; !ok { newS = append(newS, i2) m[i2] = struct{}{} } } return newS } // SpiltInt32BySize 将切片根据指定长度分割成多份 func SpiltInt32BySize(list []int32, size int) (newList [][]int32) { lens := len(list) mod := math.Ceil(float64(lens) / float64(size)) spilt := make([][]int32, 0) for i := 0; i < int(mod); i++ { tmpList := make([]int32, 0, size) if i == int(mod)-1 { tmpList = list[i*size:] } else { tmpList = list[i*size : i*size+size] } spilt = append(spilt, tmpList) } for _, sp := range spilt { newList = append(newList, sp) } return } // SpiltInt64BySize 将切片根据指定长度分割成多份 func SpiltInt64BySize(oldList []int32, size int) (newList [][]int64) { var list []int64 for _, v := range oldList { list = append(list, int64(v)) } lens := len(list) mod := math.Ceil(float64(lens) / float64(size)) spilt := make([][]int64, 0) for i := 0; i < int(mod); i++ { tmpList := make([]int64, 0, size) if i == int(mod)-1 { tmpList = list[i*size:] } else { tmpList = list[i*size : i*size+size] } spilt = append(spilt, tmpList) } for _, sp := range spilt { newList = append(newList, sp) } return } // SpiltInt64BySizeFromInt64 将切片根据指定长度分割成多份 func SpiltInt64BySizeFromInt64(oldList []int64, size int) (newList [][]int64) { var list []int64 for _, v := range oldList { list = append(list, v) } lens := len(list) mod := math.Ceil(float64(lens) / float64(size)) spilt := make([][]int64, 0) for i := 0; i < int(mod); i++ { tmpList := make([]int64, 0, size) if i == int(mod)-1 { tmpList = list[i*size:] } else { tmpList = list[i*size : i*size+size] } spilt = append(spilt, tmpList) } for _, sp := range spilt { newList = append(newList, sp) } return } // ConvertTimeUnit 转换时间单位 func ConvertTimeUnit(second int64) (newTime string) { d := second / (3600 * 24) h := (second % (3600 * 24)) / 3600 m := ((second % (3600 * 24)) % 3600) / 60 if d > 0 { if h == 0 && m == 0 { newTime = fmt.Sprintf("%v天", d) } else { newTime = fmt.Sprintf("%v天%v小时%v分钟", d, h, m) } } else { if h != '0' { if m == '0' { newTime = fmt.Sprintf("%v小时", h) } else { newTime = fmt.Sprintf("%v小时%v分钟", h, m) } } else { newTime = fmt.Sprintf("%v分", m) } } return } // SplitStringsBySize 将一个大的切片按指定的长度分割成多个小的切片 func SplitStringsBySize(slice []string, chunkSize int) [][]string { if chunkSize <= 0 { return nil } var chunks [][]string for i := 0; i < len(slice); i += chunkSize { end := i + chunkSize if end > len(slice) { end = len(slice) } chunks = append(chunks, slice[i:end]) } return chunks }