This commit is contained in:
lingxin 2024-10-22 13:55:38 +08:00
parent fa00f01a95
commit c5623e401b
3 changed files with 147 additions and 1 deletions

1
go.mod
View File

@ -29,6 +29,7 @@ require (
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect

2
go.sum
View File

@ -60,6 +60,8 @@ github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYr
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

View File

@ -1,7 +1,150 @@
package lxutils
import "fmt"
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
}