123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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
- }
|