format.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package utility
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "github.com/sirupsen/logrus"
  8. "github.com/spf13/cast"
  9. "math"
  10. "time"
  11. )
  12. func FormatInt64ToTimeStr(dateNum int64) string {
  13. stamp := time.Unix(dateNum, 0)
  14. return stamp.Format("2006-01-02")
  15. }
  16. func FormatToBytes(data interface{}) []byte {
  17. content, _ := json.Marshal(data)
  18. return content
  19. }
  20. // IsEven 判断是偶数
  21. func IsEven(num int) bool {
  22. if num%2 == 0 {
  23. return true
  24. }
  25. return false
  26. }
  27. // Round 四舍五入保留小数,默认2位
  28. func Round(value float64, args ...interface{}) (v string) {
  29. var places int32 = 2
  30. if len(args) > 0 {
  31. places = cast.ToInt32(args[0])
  32. }
  33. return decimal.NewFromFloat(value).Round(places).String()
  34. }
  35. // RoundToFloat 四舍五入保留小数,默认2位
  36. func RoundToFloat(value float64, args ...interface{}) (v float64) {
  37. var places int32 = 2
  38. if len(args) > 0 {
  39. places = cast.ToInt32(args[0])
  40. }
  41. v, _ = decimal.NewFromFloat(value).Round(places).Float64()
  42. return
  43. }
  44. func JSONMethod(content interface{}) map[string]interface{} {
  45. var name map[string]interface{}
  46. if marshalContent, err := json.Marshal(content); err != nil {
  47. logrus.Warnf("JSONMethod Marshal err:%v", err)
  48. } else {
  49. d := json.NewDecoder(bytes.NewReader(marshalContent))
  50. d.UseNumber() // 设置将float64转为一个number
  51. if err := d.Decode(&name); err != nil {
  52. fmt.Println(err)
  53. } else {
  54. for k, v := range name {
  55. name[k] = v
  56. }
  57. }
  58. }
  59. return name
  60. }
  61. func Rmu0000(s string) string {
  62. str := make([]rune, 0, len(s))
  63. for _, v := range []rune(s) {
  64. if v == 0 {
  65. continue
  66. }
  67. str = append(str, v)
  68. }
  69. return string(str)
  70. }
  71. // UniqueInt64s 切片去重
  72. func UniqueInt64s(s []int64) []int64 {
  73. m := make(map[int64]struct{}, 0)
  74. newS := make([]int64, 0)
  75. for _, i2 := range s {
  76. if _, ok := m[i2]; !ok {
  77. newS = append(newS, i2)
  78. m[i2] = struct{}{}
  79. }
  80. }
  81. return newS
  82. }
  83. // SpiltInt32BySize 将切片根据指定长度分割成多份
  84. func SpiltInt32BySize(list []int32, size int) (newList [][]int32) {
  85. lens := len(list)
  86. mod := math.Ceil(float64(lens) / float64(size))
  87. spilt := make([][]int32, 0)
  88. for i := 0; i < int(mod); i++ {
  89. tmpList := make([]int32, 0, size)
  90. if i == int(mod)-1 {
  91. tmpList = list[i*size:]
  92. } else {
  93. tmpList = list[i*size : i*size+size]
  94. }
  95. spilt = append(spilt, tmpList)
  96. }
  97. for _, sp := range spilt {
  98. newList = append(newList, sp)
  99. }
  100. return
  101. }
  102. // SpiltInt64BySize 将切片根据指定长度分割成多份
  103. func SpiltInt64BySize(oldList []int32, size int) (newList [][]int64) {
  104. var list []int64
  105. for _, v := range oldList {
  106. list = append(list, int64(v))
  107. }
  108. lens := len(list)
  109. mod := math.Ceil(float64(lens) / float64(size))
  110. spilt := make([][]int64, 0)
  111. for i := 0; i < int(mod); i++ {
  112. tmpList := make([]int64, 0, size)
  113. if i == int(mod)-1 {
  114. tmpList = list[i*size:]
  115. } else {
  116. tmpList = list[i*size : i*size+size]
  117. }
  118. spilt = append(spilt, tmpList)
  119. }
  120. for _, sp := range spilt {
  121. newList = append(newList, sp)
  122. }
  123. return
  124. }
  125. // SpiltInt64BySizeFromInt64 将切片根据指定长度分割成多份
  126. func SpiltInt64BySizeFromInt64(oldList []int64, size int) (newList [][]int64) {
  127. var list []int64
  128. for _, v := range oldList {
  129. list = append(list, v)
  130. }
  131. lens := len(list)
  132. mod := math.Ceil(float64(lens) / float64(size))
  133. spilt := make([][]int64, 0)
  134. for i := 0; i < int(mod); i++ {
  135. tmpList := make([]int64, 0, size)
  136. if i == int(mod)-1 {
  137. tmpList = list[i*size:]
  138. } else {
  139. tmpList = list[i*size : i*size+size]
  140. }
  141. spilt = append(spilt, tmpList)
  142. }
  143. for _, sp := range spilt {
  144. newList = append(newList, sp)
  145. }
  146. return
  147. }
  148. // ConvertTimeUnit 转换时间单位
  149. func ConvertTimeUnit(second int64) (newTime string) {
  150. d := second / (3600 * 24)
  151. h := (second % (3600 * 24)) / 3600
  152. m := ((second % (3600 * 24)) % 3600) / 60
  153. if d > 0 {
  154. if h == 0 && m == 0 {
  155. newTime = fmt.Sprintf("%v天", d)
  156. } else {
  157. newTime = fmt.Sprintf("%v天%v小时%v分钟", d, h, m)
  158. }
  159. } else {
  160. if h != '0' {
  161. if m == '0' {
  162. newTime = fmt.Sprintf("%v小时", h)
  163. } else {
  164. newTime = fmt.Sprintf("%v小时%v分钟", h, m)
  165. }
  166. } else {
  167. newTime = fmt.Sprintf("%v分", m)
  168. }
  169. }
  170. return
  171. }
  172. // SplitStringsBySize 将一个大的切片按指定的长度分割成多个小的切片
  173. func SplitStringsBySize(slice []string, chunkSize int) [][]string {
  174. if chunkSize <= 0 {
  175. return nil
  176. }
  177. var chunks [][]string
  178. for i := 0; i < len(slice); i += chunkSize {
  179. end := i + chunkSize
  180. if end > len(slice) {
  181. end = len(slice)
  182. }
  183. chunks = append(chunks, slice[i:end])
  184. }
  185. return chunks
  186. }