enterprise/base/util/util.go

32 lines
594 B
Go

package util
import (
"fmt"
"github.com/spf13/cast"
"math/rand"
"time"
)
func RandomStr(length int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func CutTail(str string, length int) string {
if len(str) <= length {
return str
}
return str[0:length]
}
func FloatCut(f float64) float64 {
return cast.ToFloat64(fmt.Sprintf("%.2f", f))
}