广告平台(站长下代理使用)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

375 lignes
6.8 KiB

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