lxutils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package lxutils
  2. import (
  3. "fmt"
  4. "github.com/bytedance/sonic"
  5. "github.com/shopspring/decimal"
  6. "strconv"
  7. "strings"
  8. )
  9. type H = map[string]interface{}
  10. func Trim(str interface{}) string {
  11. return strings.TrimSpace(fmt.Sprintf("%s", str))
  12. }
  13. func Aa() {
  14. fmt.Println(123)
  15. }
  16. func LxMapStruct(v interface{}, v2 interface{}) (err error) {
  17. resByre := []byte("")
  18. switch v.(type) {
  19. case string:
  20. ft := v.(string)
  21. resByre = []byte(ft)
  22. default:
  23. resByre, err = sonic.Marshal(v)
  24. if err != nil {
  25. return err
  26. }
  27. }
  28. jsonRes := sonic.Unmarshal(resByre, v2)
  29. if jsonRes != nil {
  30. return jsonRes
  31. }
  32. return nil
  33. }
  34. // uint to string
  35. func UintToStr(unitvalue uint) string {
  36. return fmt.Sprintf("%d", unitvalue) // todo 只能用这么挫的方法吗
  37. }
  38. // int to string
  39. func IntToStr(intvalue int) string {
  40. return strconv.Itoa(intvalue)
  41. }
  42. // string to int
  43. func StrToInt(str string) int {
  44. intValue, err := strconv.Atoi(str)
  45. if err != nil {
  46. intValue = int(StrToFloat(str))
  47. }
  48. return intValue
  49. }
  50. func InterfaceToStr(a interface{}) (str string) {
  51. b, _ := sonic.Marshal(a)
  52. return string(b)
  53. }
  54. func StrToFloat(str string) float64 {
  55. str = Trim(str)
  56. str = TrimQff(str)
  57. if str == "NaN" {
  58. str = "0"
  59. }
  60. intValue, err := strconv.ParseFloat(str, 64)
  61. if err != nil {
  62. intValue = 0
  63. }
  64. d := decimal.NewFromFloat(intValue)
  65. //fmt.Println(n.String())
  66. f, _ := d.Round(2).Float64()
  67. return f
  68. }
  69. func StrToUint(str string) uint {
  70. intValue, _ := strconv.Atoi(str)
  71. return uint(intValue)
  72. }
  73. // string 去千分符
  74. func TrimQff(str string) string {
  75. return strings.Replace(str, ",", "", -1)
  76. }
  77. func FloatToStr(f float64) string {
  78. // 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数
  79. // 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分)
  80. //func FormatFloat(f float64, fmt byte, prec, bitSize int) string
  81. //f := 100.12345678901234567890123456789
  82. //fmt.Println(strconv.FormatFloat(f, 'f', 5, 64))
  83. // 100.12346
  84. //fmt.Println(strconv.FormatFloat(f, 'g', 5, 64))
  85. // 100.12
  86. //fmt.Println(strconv.FormatFloat(f, 'G', 5, 64))
  87. // 100.12
  88. return strconv.FormatFloat(f, 'f', 2, 64)
  89. }
  90. // 截取两位小数
  91. func Round2(value float64) float64 {
  92. d := decimal.NewFromFloat(value)
  93. f, _ := d.Round(2).Float64()
  94. return f
  95. }
  96. // 截取三位小数
  97. func Round3(value float64) float64 {
  98. d := decimal.NewFromFloat(value)
  99. f, _ := d.Round(3).Float64()
  100. return f
  101. }
  102. func Round4(value float64) float64 {
  103. d := decimal.NewFromFloat(value)
  104. f, _ := d.Round(4).Float64()
  105. return f
  106. }
  107. func RoundStr(value float64, n ...int32) string {
  108. if value == 0 {
  109. return ""
  110. }
  111. var s int32 = 2
  112. if len(n) != 0 {
  113. s = n[0]
  114. }
  115. amount1 := decimal.NewFromFloat(value)
  116. return amount1.Round(s).StringFixed(s)
  117. }
  118. func Round1(value float64) float64 {
  119. d := decimal.NewFromFloat(value)
  120. //fmt.Println(n.String())
  121. f, _ := d.Round(1).Float64()
  122. return f
  123. }
  124. func RoundN(f float64, n int) float64 {
  125. floatStr := fmt.Sprintf("%."+strconv.Itoa(n)+"f", f)
  126. inst, _ := strconv.ParseFloat(floatStr, 64)
  127. return inst
  128. }
  129. func Round(value float64, n int32) float64 {
  130. d := decimal.NewFromFloat(value)
  131. //fmt.Println(n.String())
  132. f, _ := d.Round(n).Float64()
  133. return f
  134. }