优惠券额度包
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

convert.go 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package utils
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. )
  11. func ToString(raw interface{}, e error) (res string) {
  12. if e != nil {
  13. return ""
  14. }
  15. return AnyToString(raw)
  16. }
  17. func ToInt64(raw interface{}, e error) int64 {
  18. if e != nil {
  19. return 0
  20. }
  21. return AnyToInt64(raw)
  22. }
  23. func AnyToBool(raw interface{}) bool {
  24. switch i := raw.(type) {
  25. case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
  26. return i != 0
  27. case []byte:
  28. return i != nil
  29. case string:
  30. if i == "false" {
  31. return false
  32. }
  33. return i != ""
  34. case error:
  35. return false
  36. case nil:
  37. return true
  38. }
  39. val := fmt.Sprint(raw)
  40. val = strings.TrimLeft(val, "&")
  41. if strings.TrimLeft(val, "{}") == "" {
  42. return false
  43. }
  44. if strings.TrimLeft(val, "[]") == "" {
  45. return false
  46. }
  47. // ptr type
  48. b, err := json.Marshal(raw)
  49. if err != nil {
  50. return false
  51. }
  52. if strings.TrimLeft(string(b), "\"\"") == "" {
  53. return false
  54. }
  55. if strings.TrimLeft(string(b), "{}") == "" {
  56. return false
  57. }
  58. return true
  59. }
  60. func AnyToInt64(raw interface{}) int64 {
  61. switch i := raw.(type) {
  62. case string:
  63. res, _ := strconv.ParseInt(i, 10, 64)
  64. return res
  65. case []byte:
  66. return BytesToInt64(i)
  67. case int:
  68. return int64(i)
  69. case int64:
  70. return i
  71. case uint:
  72. return int64(i)
  73. case uint8:
  74. return int64(i)
  75. case uint16:
  76. return int64(i)
  77. case uint32:
  78. return int64(i)
  79. case uint64:
  80. return int64(i)
  81. case int8:
  82. return int64(i)
  83. case int16:
  84. return int64(i)
  85. case int32:
  86. return int64(i)
  87. case float32:
  88. return int64(i)
  89. case float64:
  90. return int64(i)
  91. case error:
  92. return 0
  93. case bool:
  94. if i {
  95. return 1
  96. }
  97. return 0
  98. }
  99. return 0
  100. }
  101. func AnyToString(raw interface{}) string {
  102. switch i := raw.(type) {
  103. case []byte:
  104. return string(i)
  105. case int:
  106. return strconv.FormatInt(int64(i), 10)
  107. case int64:
  108. return strconv.FormatInt(i, 10)
  109. case float32:
  110. return Float64ToStr(float64(i))
  111. case float64:
  112. return Float64ToStr(i)
  113. case uint:
  114. return strconv.FormatInt(int64(i), 10)
  115. case uint8:
  116. return strconv.FormatInt(int64(i), 10)
  117. case uint16:
  118. return strconv.FormatInt(int64(i), 10)
  119. case uint32:
  120. return strconv.FormatInt(int64(i), 10)
  121. case uint64:
  122. return strconv.FormatInt(int64(i), 10)
  123. case int8:
  124. return strconv.FormatInt(int64(i), 10)
  125. case int16:
  126. return strconv.FormatInt(int64(i), 10)
  127. case int32:
  128. return strconv.FormatInt(int64(i), 10)
  129. case string:
  130. return i
  131. case error:
  132. return i.Error()
  133. case bool:
  134. return strconv.FormatBool(i)
  135. }
  136. return fmt.Sprintf("%#v", raw)
  137. }
  138. func AnyToFloat64(raw interface{}) float64 {
  139. switch i := raw.(type) {
  140. case []byte:
  141. f, _ := strconv.ParseFloat(string(i), 64)
  142. return f
  143. case int:
  144. return float64(i)
  145. case int64:
  146. return float64(i)
  147. case float32:
  148. return float64(i)
  149. case float64:
  150. return i
  151. case uint:
  152. return float64(i)
  153. case uint8:
  154. return float64(i)
  155. case uint16:
  156. return float64(i)
  157. case uint32:
  158. return float64(i)
  159. case uint64:
  160. return float64(i)
  161. case int8:
  162. return float64(i)
  163. case int16:
  164. return float64(i)
  165. case int32:
  166. return float64(i)
  167. case string:
  168. f, _ := strconv.ParseFloat(i, 64)
  169. return f
  170. case bool:
  171. if i {
  172. return 1
  173. }
  174. }
  175. return 0
  176. }
  177. func ToByte(raw interface{}, e error) []byte {
  178. if e != nil {
  179. return []byte{}
  180. }
  181. switch i := raw.(type) {
  182. case string:
  183. return []byte(i)
  184. case int:
  185. return Int64ToBytes(int64(i))
  186. case int64:
  187. return Int64ToBytes(i)
  188. case float32:
  189. return Float32ToByte(i)
  190. case float64:
  191. return Float64ToByte(i)
  192. case uint:
  193. return Int64ToBytes(int64(i))
  194. case uint8:
  195. return Int64ToBytes(int64(i))
  196. case uint16:
  197. return Int64ToBytes(int64(i))
  198. case uint32:
  199. return Int64ToBytes(int64(i))
  200. case uint64:
  201. return Int64ToBytes(int64(i))
  202. case int8:
  203. return Int64ToBytes(int64(i))
  204. case int16:
  205. return Int64ToBytes(int64(i))
  206. case int32:
  207. return Int64ToBytes(int64(i))
  208. case []byte:
  209. return i
  210. case error:
  211. return []byte(i.Error())
  212. case bool:
  213. if i {
  214. return []byte("true")
  215. }
  216. return []byte("false")
  217. }
  218. return []byte(fmt.Sprintf("%#v", raw))
  219. }
  220. func Int64ToBytes(i int64) []byte {
  221. var buf = make([]byte, 8)
  222. binary.BigEndian.PutUint64(buf, uint64(i))
  223. return buf
  224. }
  225. func BytesToInt64(buf []byte) int64 {
  226. return int64(binary.BigEndian.Uint64(buf))
  227. }
  228. func StrToInt(s string) int {
  229. res, _ := strconv.Atoi(s)
  230. return res
  231. }
  232. func StrToInt64(s string) int64 {
  233. res, _ := strconv.ParseInt(s, 10, 64)
  234. return res
  235. }
  236. func Float32ToByte(float float32) []byte {
  237. bits := math.Float32bits(float)
  238. bytes := make([]byte, 4)
  239. binary.LittleEndian.PutUint32(bytes, bits)
  240. return bytes
  241. }
  242. func ByteToFloat32(bytes []byte) float32 {
  243. bits := binary.LittleEndian.Uint32(bytes)
  244. return math.Float32frombits(bits)
  245. }
  246. func Float64ToByte(float float64) []byte {
  247. bits := math.Float64bits(float)
  248. bytes := make([]byte, 8)
  249. binary.LittleEndian.PutUint64(bytes, bits)
  250. return bytes
  251. }
  252. func ByteToFloat64(bytes []byte) float64 {
  253. bits := binary.LittleEndian.Uint64(bytes)
  254. return math.Float64frombits(bits)
  255. }
  256. func Float64ToStr(f float64) string {
  257. return strconv.FormatFloat(f, 'f', 2, 64)
  258. }
  259. func Float64ToStrWithPoint(f float64, point int) string {
  260. return strconv.FormatFloat(f, 'f', point, 64)
  261. }
  262. func Float64ToStrByPrec(f float64, prec int) string {
  263. return strconv.FormatFloat(f, 'f', prec, 64)
  264. }
  265. func Float64ToStrPrec1(f float64) string {
  266. return strconv.FormatFloat(f, 'f', 1, 64)
  267. }
  268. func Float32ToStr(f float32) string {
  269. return Float64ToStr(float64(f))
  270. }
  271. func StrToFloat64(s string) float64 {
  272. res, err := strconv.ParseFloat(s, 64)
  273. if err != nil {
  274. return 0
  275. }
  276. return res
  277. }
  278. func StrToFloat32(s string) float32 {
  279. res, err := strconv.ParseFloat(s, 32)
  280. if err != nil {
  281. return 0
  282. }
  283. return float32(res)
  284. }
  285. func StrToBool(s string) bool {
  286. b, _ := strconv.ParseBool(s)
  287. return b
  288. }
  289. func BoolToStr(b bool) string {
  290. if b {
  291. return "true"
  292. }
  293. return "false"
  294. }
  295. func FloatToInt64(f float64) int64 {
  296. return int64(f)
  297. }
  298. func IntToStr(i int) string {
  299. return strconv.Itoa(i)
  300. }
  301. func Int64ToStr(i int64) string {
  302. return strconv.FormatInt(i, 10)
  303. }
  304. func IsNil(i interface{}) bool {
  305. vi := reflect.ValueOf(i)
  306. if vi.Kind() == reflect.Ptr {
  307. return vi.IsNil()
  308. }
  309. return false
  310. }
  311. func StrToFormat(s string, prec int) string {
  312. ex := strings.Split(s, ".")
  313. if len(ex) == 2 {
  314. if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了
  315. return ex[0]
  316. }
  317. //看取多少位
  318. str := ex[1]
  319. str1 := str
  320. if prec < len(str) {
  321. str1 = str[0:prec]
  322. } else {
  323. for i := 0; i < prec-len(str); i++ {
  324. str1 += "0"
  325. }
  326. }
  327. return ex[0] + "." + str1
  328. }
  329. return s
  330. }