广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert.go 6.9 KiB

3 maanden geleden
1 maand geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
3 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. f = float64(int(f*100)) / 100
  257. return strconv.FormatFloat(f, 'f', 2, 64)
  258. }
  259. func Float64ToStrPrec1(f float64) string {
  260. return strconv.FormatFloat(f, 'f', 1, 64)
  261. }
  262. func Float64ToStrPrec4(f float64) string {
  263. return strconv.FormatFloat(f, 'f', 1, 64)
  264. }
  265. func Float32ToStr(f float32) string {
  266. return Float64ToStr(float64(f))
  267. }
  268. func StrToFloat64(s string) float64 {
  269. res, err := strconv.ParseFloat(s, 64)
  270. if err != nil {
  271. return 0
  272. }
  273. return res
  274. }
  275. func StrToFloat32(s string) float32 {
  276. res, err := strconv.ParseFloat(s, 32)
  277. if err != nil {
  278. return 0
  279. }
  280. return float32(res)
  281. }
  282. func StrToBool(s string) bool {
  283. b, _ := strconv.ParseBool(s)
  284. return b
  285. }
  286. func BoolToStr(b bool) string {
  287. if b {
  288. return "true"
  289. }
  290. return "false"
  291. }
  292. func FloatToInt64(f float64) int64 {
  293. return int64(f)
  294. }
  295. func IntToStr(i int) string {
  296. return strconv.Itoa(i)
  297. }
  298. func Int64ToStr(i int64) string {
  299. return strconv.FormatInt(i, 10)
  300. }
  301. func Float64ToStrByPrec(f float64, prec int) string {
  302. return strconv.FormatFloat(f, 'f', prec, 64)
  303. }
  304. func GetPrec(sum, commPrec string) string {
  305. if sum == "" {
  306. sum = "0"
  307. }
  308. sum = StrToFormat(sum, StrToInt(commPrec))
  309. ex := strings.Split(sum, ".")
  310. if len(ex) == 2 {
  311. if StrToFloat64(ex[1]) == 0 {
  312. sum = ex[0]
  313. } else {
  314. val := Float64ToStrByPrec(StrToFloat64(ex[1]), 0)
  315. keyMax := 0
  316. for i := 0; i < len(val); i++ {
  317. ch := string(val[i])
  318. fmt.Println(StrToInt(ch))
  319. if StrToInt(ch) > 0 {
  320. keyMax = i
  321. }
  322. }
  323. valNew := val[0 : keyMax+1]
  324. sum = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
  325. }
  326. }
  327. return sum
  328. }
  329. func StrToFormat(s string, prec int) string {
  330. ex := strings.Split(s, ".")
  331. if len(ex) == 2 {
  332. if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了
  333. return ex[0]
  334. }
  335. //看取多少位
  336. str := ex[1]
  337. str1 := str
  338. if prec < len(str) {
  339. str1 = str[0:prec]
  340. } else {
  341. for i := 0; i < prec-len(str); i++ {
  342. str1 += "0"
  343. }
  344. }
  345. if prec > 0 {
  346. return ex[0] + "." + str1
  347. } else {
  348. return ex[0]
  349. }
  350. }
  351. return s
  352. }