lxutils/lxutils.go
2025-08-11 16:47:30 +08:00

179 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lxutils
import (
"fmt"
"github.com/bytedance/sonic"
"github.com/shopspring/decimal"
"strconv"
"strings"
)
type H = map[string]interface{}
func Trim(str interface{}) string {
return strings.TrimSpace(fmt.Sprintf("%s", str))
}
func Aa() {
fmt.Println(123)
}
func LxMapStruct(v interface{}, v2 interface{}) (err error) {
resByre := []byte("")
switch v.(type) {
case string:
ft := v.(string)
resByre = []byte(ft)
default:
resByre, err = sonic.Marshal(v)
if err != nil {
return err
}
}
jsonRes := sonic.Unmarshal(resByre, v2)
if jsonRes != nil {
return jsonRes
}
return nil
}
// uint to string
func UintToStr(unitvalue uint) string {
return fmt.Sprintf("%d", unitvalue) // todo 只能用这么挫的方法吗
}
// int to string
func IntToStr(intvalue int) string {
return strconv.Itoa(intvalue)
}
// string to int
func StrToInt(str string) int {
intValue, err := strconv.Atoi(str)
if err != nil {
intValue = int(StrToFloat(str))
}
return intValue
}
func InterfaceToStr(a interface{}) (str string) {
b, _ := sonic.Marshal(a)
return string(b)
}
func StrToFloat(str string) float64 {
str = Trim(str)
str = TrimQff(str)
if str == "NaN" {
str = "0"
}
intValue, err := strconv.ParseFloat(str, 64)
if err != nil {
intValue = 0
}
d := decimal.NewFromFloat(intValue)
//fmt.Println(n.String())
f, _ := d.Round(2).Float64()
return f
}
func StrToUint(str string) uint {
intValue, _ := strconv.Atoi(str)
return uint(intValue)
}
// string 去千分符
func TrimQff(str string) string {
return strings.Replace(str, ",", "", -1)
}
func FloatToStr(f float64) string {
// 如果格式标记为 'e''E'和'f',则 prec 表示小数点后的数字位数
// 如果格式标记为 'g''G',则 prec 表示总的数字位数(整数部分+小数部分)
//func FormatFloat(f float64, fmt byte, prec, bitSize int) string
//f := 100.12345678901234567890123456789
//fmt.Println(strconv.FormatFloat(f, 'f', 5, 64))
// 100.12346
//fmt.Println(strconv.FormatFloat(f, 'g', 5, 64))
// 100.12
//fmt.Println(strconv.FormatFloat(f, 'G', 5, 64))
// 100.12
return strconv.FormatFloat(f, 'f', 2, 64)
}
// 截取两位小数
func Round2(value float64) float64 {
d := decimal.NewFromFloat(value)
f, _ := d.Round(2).Float64()
return f
}
// 截取三位小数
func Round3(value float64) float64 {
d := decimal.NewFromFloat(value)
f, _ := d.Round(3).Float64()
return f
}
func Round4(value float64) float64 {
d := decimal.NewFromFloat(value)
f, _ := d.Round(4).Float64()
return f
}
func RoundStr(value float64, n ...int32) string {
if value == 0 {
return ""
}
var s int32 = 2
if len(n) != 0 {
s = n[0]
}
amount1 := decimal.NewFromFloat(value)
return amount1.Round(s).StringFixed(s)
}
func Round1(value float64) float64 {
d := decimal.NewFromFloat(value)
//fmt.Println(n.String())
f, _ := d.Round(1).Float64()
return f
}
func RoundN(f float64, n int) float64 {
floatStr := fmt.Sprintf("%."+strconv.Itoa(n)+"f", f)
inst, _ := strconv.ParseFloat(floatStr, 64)
return inst
}
func Round(value float64, n int32) float64 {
d := decimal.NewFromFloat(value)
//fmt.Println(n.String())
f, _ := d.Round(n).Float64()
return f
}
// 判断在不在数组中
func CheckInArr[T int | string | uint](target T, strArray []T) bool {
for _, element := range strArray {
if target == element {
return true
}
}
return false
}
// 数组去重
func RemoveRepeatedElement[T int | string | uint](arr []T) (newArr []T) {
newArr = make([]T, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := 0; j < len(newArr); j++ {
if arr[i] == newArr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return
}