常用类型及数据操作方法
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.

169 lines
3.3 KiB

3 years ago
3 years ago
  1. package helper
  2. import (
  3. "fmt"
  4. "math/big"
  5. "strconv"
  6. "strings"
  7. )
  8. //字节自动转换为B/KB/MB/GB
  9. func ByteToUnitSize(Size interface{}) (float64, string) {
  10. var unit string
  11. var iFloat, mb, gb float64 = 0, 1024, 1024 * 1024
  12. iSize, _ := ToFloat64(Size)
  13. rSize := iSize / 1024
  14. if rSize >= 0 && rSize < 1 {
  15. iFloat, unit = iSize, "B"
  16. } else if rSize >= 1 && rSize < mb {
  17. iFloat, unit = rSize, "KB"
  18. } else if rSize >= mb && rSize < gb {
  19. iFloat, unit = rSize/1024, "MB"
  20. } else if rSize >= gb {
  21. iFloat, unit = rSize/1024/1024, "GB"
  22. }
  23. return iFloat, unit
  24. }
  25. /**
  26. * 数值相加
  27. * @param str 加数加数...
  28. * 2020/05/22
  29. * gz
  30. */
  31. func FloatAdd(str ...interface{}) float64 {
  32. length := len(str)
  33. if length < 1 {
  34. return 0
  35. }
  36. var i int = 0
  37. var result, tmp float64 = 0, 0
  38. var err error
  39. for i < length {
  40. tmp, err = ToFloat64(str[i])
  41. if err == nil {
  42. result, _ = new(big.Float).Add(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
  43. }
  44. i++
  45. }
  46. result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
  47. return result
  48. }
  49. /**
  50. * float相减
  51. * @param str 被减数减数...
  52. * 2020/05/22
  53. * gz
  54. */
  55. func FloatSub(str ...interface{}) float64 {
  56. length := len(str)
  57. if length < 1 {
  58. return 0
  59. }
  60. var i int = 1
  61. result, err := ToFloat64(str[0])
  62. var tmp float64 = 0
  63. for i < length {
  64. tmp, err = ToFloat64(str[i])
  65. if err == nil {
  66. result, _ = new(big.Float).Sub(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
  67. }
  68. i++
  69. }
  70. result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
  71. return result
  72. }
  73. /**
  74. * float相乘
  75. * @param str 被除数除数保留小数位数
  76. * 2020/05/25
  77. * gz
  78. */
  79. func FloatMul(str ...interface{}) float64 {
  80. if len(str) < 2 {
  81. return 0
  82. }
  83. places := "2" //默认两位小数
  84. if len(str) > 2 {
  85. places = ToString(str[2])
  86. }
  87. float_1, _ := ToFloat64(str[0])
  88. float_2, _ := ToFloat64(str[1])
  89. ret, _ := new(big.Float).Mul(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()
  90. ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
  91. return ret
  92. }
  93. /**
  94. * float相除
  95. * @param str 被除数除数保留小数位数
  96. * 2020/05/25
  97. * gz
  98. */
  99. func FloatQuo(str ...interface{}) float64 {
  100. if len(str) < 2 {
  101. return 0
  102. }
  103. places := "2" //默认两位小数
  104. if len(str) > 2 {
  105. places = ToString(str[2])
  106. }
  107. float_1, _ := ToFloat64(str[0])
  108. float_2, _ := ToFloat64(str[1])
  109. if float_2 <= 0 {
  110. return 0
  111. }
  112. ret, _ := new(big.Float).Quo(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()
  113. ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
  114. return ret
  115. }
  116. /**
  117. * 十进制转十六进制
  118. */
  119. func DecToHex(ten int) string {
  120. m := 0
  121. hex := make([]int, 0)
  122. for {
  123. m = ten % 16
  124. ten = ten / 16
  125. if ten == 0 {
  126. hex = append(hex, m)
  127. break
  128. }
  129. hex = append(hex, m)
  130. }
  131. hexStr := []string{}
  132. for i := len(hex) - 1; i >= 0; i-- {
  133. if hex[i] >= 10 {
  134. hexStr = append(hexStr, fmt.Sprintf("%c", 'A'+hex[i]-10))
  135. } else {
  136. hexStr = append(hexStr, fmt.Sprintf("%d", hex[i]))
  137. }
  138. }
  139. return strings.Join(hexStr, "")
  140. }
  141. /**
  142. * 十六进制转十进制
  143. */
  144. func HexToDec(val string) (int, error) {
  145. n, err := strconv.ParseUint(val, 16, 64)
  146. if err != nil {
  147. return 0, err
  148. }
  149. return int(n), nil
  150. }