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

131 lines
2.6 KiB

3 years ago
  1. package helper
  2. import (
  3. "fmt"
  4. "math/big"
  5. "strconv"
  6. )
  7. //字节自动转换为B/KB/MB/GB
  8. func ByteToUnitSize(Size interface{}) (float64, string) {
  9. var unit string
  10. var iFloat, mb, gb float64 = 0, 1024, 1024 * 1024
  11. iSize, _ := ToFloat64(Size)
  12. rSize := iSize / 1024
  13. if rSize >= 0 && rSize < 1 {
  14. iFloat, unit = iSize, "B"
  15. } else if rSize >= 1 && rSize < mb {
  16. iFloat, unit = rSize, "KB"
  17. } else if rSize >= mb && rSize < gb {
  18. iFloat, unit = rSize/1024, "MB"
  19. } else if rSize >= gb {
  20. iFloat, unit = rSize/1024/1024, "GB"
  21. }
  22. return iFloat, unit
  23. }
  24. /**
  25. * 数值相加
  26. * @param str 加数加数...
  27. * 2020/05/22
  28. * gz
  29. */
  30. func FloatAdd(str ...interface{}) float64 {
  31. length := len(str)
  32. if length < 1 {
  33. return 0
  34. }
  35. var i int = 0
  36. var result, tmp float64 = 0, 0
  37. var err error
  38. for i < length {
  39. tmp, err = ToFloat64(str[i])
  40. if err == nil {
  41. result, _ = new(big.Float).Add(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
  42. }
  43. i++
  44. }
  45. result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
  46. return result
  47. }
  48. /**
  49. * float相减
  50. * @param str 被减数减数...
  51. * 2020/05/22
  52. * gz
  53. */
  54. func FloatSub(str ...interface{}) float64 {
  55. length := len(str)
  56. if length < 1 {
  57. return 0
  58. }
  59. var i int = 1
  60. result, err := ToFloat64(str[0])
  61. var tmp float64 = 0
  62. for i < length {
  63. tmp, err = ToFloat64(str[i])
  64. if err == nil {
  65. result, _ = new(big.Float).Sub(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
  66. }
  67. i++
  68. }
  69. result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
  70. return result
  71. }
  72. /**
  73. * float相乘
  74. * @param str 被除数除数保留小数位数
  75. * 2020/05/25
  76. * gz
  77. */
  78. func FloatMul(str ...interface{}) float64 {
  79. if len(str) < 2 {
  80. return 0
  81. }
  82. places := "2" //默认两位小数
  83. if len(str) > 2 {
  84. places = ToString(str[2])
  85. }
  86. float_1, _ := ToFloat64(str[0])
  87. float_2, _ := ToFloat64(str[1])
  88. ret, _ := new(big.Float).Mul(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()
  89. ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
  90. return ret
  91. }
  92. /**
  93. * float相除
  94. * @param str 被除数除数保留小数位数
  95. * 2020/05/25
  96. * gz
  97. */
  98. func FloatQuo(str ...interface{}) float64 {
  99. if len(str) < 2 {
  100. return 0
  101. }
  102. places := "2" //默认两位小数
  103. if len(str) > 2 {
  104. places = ToString(str[2])
  105. }
  106. float_1, _ := ToFloat64(str[0])
  107. float_2, _ := ToFloat64(str[1])
  108. if float_2 <= 0 {
  109. return 0
  110. }
  111. ret, _ := new(big.Float).Quo(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()
  112. ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
  113. return ret
  114. }