附近小店
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

convert.go 8.2 KiB

hace 1 mes
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package utils
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "math"
  9. "strconv"
  10. "strings"
  11. )
  12. func ToString(raw interface{}, e error) (res string) {
  13. if e != nil {
  14. return ""
  15. }
  16. return AnyToString(raw)
  17. }
  18. func ToInt64(raw interface{}, e error) int64 {
  19. if e != nil {
  20. return 0
  21. }
  22. return AnyToInt64(raw)
  23. }
  24. func Float64ToStrPrec4(f float64) string {
  25. return strconv.FormatFloat(f, 'f', 4, 64)
  26. }
  27. func AnyToBool(raw interface{}) bool {
  28. switch i := raw.(type) {
  29. case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
  30. return i != 0
  31. case []byte:
  32. return i != nil
  33. case string:
  34. if i == "false" {
  35. return false
  36. }
  37. return i != ""
  38. case error:
  39. return false
  40. case nil:
  41. return true
  42. }
  43. val := fmt.Sprint(raw)
  44. val = strings.TrimLeft(val, "&")
  45. if strings.TrimLeft(val, "{}") == "" {
  46. return false
  47. }
  48. if strings.TrimLeft(val, "[]") == "" {
  49. return false
  50. }
  51. // ptr type
  52. b, err := json.Marshal(raw)
  53. if err != nil {
  54. return false
  55. }
  56. if strings.TrimLeft(string(b), "\"\"") == "" {
  57. return false
  58. }
  59. if strings.TrimLeft(string(b), "{}") == "" {
  60. return false
  61. }
  62. return true
  63. }
  64. func AnyToInt64(raw interface{}) int64 {
  65. switch i := raw.(type) {
  66. case string:
  67. res, _ := strconv.ParseInt(i, 10, 64)
  68. return res
  69. case []byte:
  70. return BytesToInt64(i)
  71. case int:
  72. return int64(i)
  73. case int64:
  74. return i
  75. case uint:
  76. return int64(i)
  77. case uint8:
  78. return int64(i)
  79. case uint16:
  80. return int64(i)
  81. case uint32:
  82. return int64(i)
  83. case uint64:
  84. return int64(i)
  85. case int8:
  86. return int64(i)
  87. case int16:
  88. return int64(i)
  89. case int32:
  90. return int64(i)
  91. case float32:
  92. return int64(i)
  93. case float64:
  94. return int64(i)
  95. case error:
  96. return 0
  97. case bool:
  98. if i {
  99. return 1
  100. }
  101. return 0
  102. }
  103. return 0
  104. }
  105. func AnyToString(raw interface{}) string {
  106. switch i := raw.(type) {
  107. case []byte:
  108. return string(i)
  109. case int:
  110. return strconv.FormatInt(int64(i), 10)
  111. case int64:
  112. return strconv.FormatInt(i, 10)
  113. case float32:
  114. return Float64ToStr(float64(i))
  115. case float64:
  116. return Float64ToStr(i)
  117. case uint:
  118. return strconv.FormatInt(int64(i), 10)
  119. case uint8:
  120. return strconv.FormatInt(int64(i), 10)
  121. case uint16:
  122. return strconv.FormatInt(int64(i), 10)
  123. case uint32:
  124. return strconv.FormatInt(int64(i), 10)
  125. case uint64:
  126. return strconv.FormatInt(int64(i), 10)
  127. case int8:
  128. return strconv.FormatInt(int64(i), 10)
  129. case int16:
  130. return strconv.FormatInt(int64(i), 10)
  131. case int32:
  132. return strconv.FormatInt(int64(i), 10)
  133. case string:
  134. return i
  135. case error:
  136. return i.Error()
  137. case bool:
  138. return strconv.FormatBool(i)
  139. }
  140. return fmt.Sprintf("%#v", raw)
  141. }
  142. func AnyToFloat64(raw interface{}) float64 {
  143. switch i := raw.(type) {
  144. case []byte:
  145. f, _ := strconv.ParseFloat(string(i), 64)
  146. return f
  147. case int:
  148. return float64(i)
  149. case int64:
  150. return float64(i)
  151. case float32:
  152. return float64(i)
  153. case float64:
  154. return i
  155. case uint:
  156. return float64(i)
  157. case uint8:
  158. return float64(i)
  159. case uint16:
  160. return float64(i)
  161. case uint32:
  162. return float64(i)
  163. case uint64:
  164. return float64(i)
  165. case int8:
  166. return float64(i)
  167. case int16:
  168. return float64(i)
  169. case int32:
  170. return float64(i)
  171. case string:
  172. f, _ := strconv.ParseFloat(i, 64)
  173. return f
  174. case bool:
  175. if i {
  176. return 1
  177. }
  178. }
  179. return 0
  180. }
  181. func ToByte(raw interface{}, e error) []byte {
  182. if e != nil {
  183. return []byte{}
  184. }
  185. switch i := raw.(type) {
  186. case string:
  187. return []byte(i)
  188. case int:
  189. return Int64ToBytes(int64(i))
  190. case int64:
  191. return Int64ToBytes(i)
  192. case float32:
  193. return Float32ToByte(i)
  194. case float64:
  195. return Float64ToByte(i)
  196. case uint:
  197. return Int64ToBytes(int64(i))
  198. case uint8:
  199. return Int64ToBytes(int64(i))
  200. case uint16:
  201. return Int64ToBytes(int64(i))
  202. case uint32:
  203. return Int64ToBytes(int64(i))
  204. case uint64:
  205. return Int64ToBytes(int64(i))
  206. case int8:
  207. return Int64ToBytes(int64(i))
  208. case int16:
  209. return Int64ToBytes(int64(i))
  210. case int32:
  211. return Int64ToBytes(int64(i))
  212. case []byte:
  213. return i
  214. case error:
  215. return []byte(i.Error())
  216. case bool:
  217. if i {
  218. return []byte("true")
  219. }
  220. return []byte("false")
  221. }
  222. return []byte(fmt.Sprintf("%#v", raw))
  223. }
  224. func Int64ToBytes(i int64) []byte {
  225. var buf = make([]byte, 8)
  226. binary.BigEndian.PutUint64(buf, uint64(i))
  227. return buf
  228. }
  229. func BytesToInt64(buf []byte) int64 {
  230. return int64(binary.BigEndian.Uint64(buf))
  231. }
  232. func StrToInt(s string) int {
  233. res, _ := strconv.Atoi(s)
  234. return res
  235. }
  236. func StrToInt64(s string) int64 {
  237. res, _ := strconv.ParseInt(s, 10, 64)
  238. return res
  239. }
  240. func Float32ToByte(float float32) []byte {
  241. bits := math.Float32bits(float)
  242. bytes := make([]byte, 4)
  243. binary.LittleEndian.PutUint32(bytes, bits)
  244. return bytes
  245. }
  246. func ByteToFloat32(bytes []byte) float32 {
  247. bits := binary.LittleEndian.Uint32(bytes)
  248. return math.Float32frombits(bits)
  249. }
  250. func Float64ToByte(float float64) []byte {
  251. bits := math.Float64bits(float)
  252. bytes := make([]byte, 8)
  253. binary.LittleEndian.PutUint64(bytes, bits)
  254. return bytes
  255. }
  256. func ByteToFloat64(bytes []byte) float64 {
  257. bits := binary.LittleEndian.Uint64(bytes)
  258. return math.Float64frombits(bits)
  259. }
  260. func Float64ToStr(f float64) string {
  261. return strconv.FormatFloat(f, 'f', 2, 64)
  262. }
  263. func Float64ToStrPrec1(f float64) string {
  264. return strconv.FormatFloat(f, 'f', 1, 64)
  265. }
  266. func Float64ToStrByPrec(f float64, prec int) string {
  267. return strconv.FormatFloat(f, 'f', prec, 64)
  268. }
  269. func Float32ToStrByPrec(f float32, prec int) string {
  270. return Float64ToStrByPrec(float64(f), prec)
  271. }
  272. func GetFloatByOne(count, num, mul float64, prec int) string {
  273. return Float64ToStrByPrec(float64(int(float64(count)/num*mul))/mul, prec)
  274. }
  275. func Float32ToStr(f float32) string {
  276. return Float64ToStr(float64(f))
  277. }
  278. func StrToFloat64(s string) float64 {
  279. res, err := strconv.ParseFloat(s, 64)
  280. if err != nil {
  281. return 0
  282. }
  283. return res
  284. }
  285. func StrToFormatByType(c *gin.Context, s string, types string) string {
  286. commPrec := "2"
  287. if types == "commission" {
  288. commPrec = c.GetString("commission_prec")
  289. }
  290. if types == "integral" {
  291. commPrec = c.GetString("integral_prec")
  292. }
  293. if s == "" {
  294. s = "0"
  295. }
  296. s = StrToFormat(c, s, utils.StrToInt(commPrec))
  297. ex := strings.Split(s, ".")
  298. if len(ex) == 2 && c.GetString("is_show_point") != "1" {
  299. if utils.StrToFloat64(ex[1]) == 0 {
  300. s = ex[0]
  301. } else {
  302. val := utils.Float64ToStrByPrec(utils.StrToFloat64(ex[1]), 0)
  303. keyMax := 0
  304. for i := 0; i < len(val); i++ {
  305. ch := string(val[i])
  306. fmt.Println(utils.StrToInt(ch))
  307. if utils.StrToInt(ch) > 0 {
  308. keyMax = i
  309. }
  310. }
  311. valNew := val[0 : keyMax+1]
  312. s = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
  313. }
  314. }
  315. return s
  316. }
  317. func StrToFormat(c *gin.Context, s string, prec int) string {
  318. ex := strings.Split(s, ".")
  319. if len(ex) == 2 {
  320. if StrToFloat64(ex[1]) == 0 && c.GetString("is_show_point") != "1" { //小数点后面为空就是不要小数点了
  321. return ex[0]
  322. }
  323. //看取多少位
  324. str := ex[1]
  325. str1 := str
  326. if prec < len(str) {
  327. str1 = str[0:prec]
  328. } else {
  329. for i := 0; i < prec-len(str); i++ {
  330. str1 += "0"
  331. }
  332. }
  333. if prec > 0 {
  334. return ex[0] + "." + str1
  335. } else {
  336. return ex[0]
  337. }
  338. }
  339. return s
  340. }
  341. func StrToFormatEg(s string, prec int, is_show_point string) string {
  342. ex := strings.Split(s, ".")
  343. if len(ex) == 2 {
  344. if StrToFloat64(ex[1]) == 0 && is_show_point != "1" { //小数点后面为空就是不要小数点了
  345. return ex[0]
  346. }
  347. //看取多少位
  348. str := ex[1]
  349. str1 := str
  350. if prec < len(str) {
  351. str1 = str[0:prec]
  352. } else {
  353. for i := 0; i < prec-len(str); i++ {
  354. str1 += "0"
  355. }
  356. }
  357. if prec > 0 {
  358. return ex[0] + "." + str1
  359. } else {
  360. return ex[0]
  361. }
  362. }
  363. return s
  364. }
  365. func StrToFloat32(s string) float32 {
  366. res, err := strconv.ParseFloat(s, 32)
  367. if err != nil {
  368. return 0
  369. }
  370. return float32(res)
  371. }
  372. func StrToBool(s string) bool {
  373. b, _ := strconv.ParseBool(s)
  374. return b
  375. }
  376. func BoolToStr(b bool) string {
  377. if b {
  378. return "true"
  379. }
  380. return "false"
  381. }
  382. func FloatToInt64(f float64) int64 {
  383. return int64(f)
  384. }
  385. func IntToStr(i int) string {
  386. return strconv.Itoa(i)
  387. }
  388. func Int64ToStr(i int64) string {
  389. return strconv.FormatInt(i, 10)
  390. }
  391. func IntToFloat64(i int) float64 {
  392. s := strconv.Itoa(i)
  393. res, err := strconv.ParseFloat(s, 64)
  394. if err != nil {
  395. return 0
  396. }
  397. return res
  398. }
  399. func Int64ToFloat64(i int64) float64 {
  400. s := strconv.FormatInt(i, 10)
  401. res, err := strconv.ParseFloat(s, 64)
  402. if err != nil {
  403. return 0
  404. }
  405. return res
  406. }