c_money.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package clean
  2. import (
  3. "fmt"
  4. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  5. "math"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. )
  10. var (
  11. moneyReg1 = regexp.MustCompile("([\\s ,]+)")
  12. moneyReg2 = regexp.MustCompile("^([0-9.]+)E([1-7])$")
  13. numReg1 = regexp.MustCompile("([0-9\\.]+)")
  14. )
  15. // 清洗金额
  16. func CleanMoney(money interface{}) float64 {
  17. tmpstr := ""
  18. if _, ok := money.(float64); ok {
  19. tmpstr = fmt.Sprintf("%f", money)
  20. } else {
  21. tmpstr = util.ObjToString(money)
  22. }
  23. //去除空格
  24. tmpstr = moneyReg1.ReplaceAllString(tmpstr, "")
  25. //科学计数法
  26. if moneyReg2.MatchString(tmpstr) {
  27. price := util.Float64All(moneyReg2.ReplaceAllString(tmpstr, "${1}"))
  28. if unit := util.Float64All(moneyReg2.ReplaceAllString(tmpstr, "${2}")); unit > 0.0 && price > 0.0 {
  29. tmpstr = fmt.Sprintf("%f", math.Pow(10, unit)*price)
  30. }
  31. }
  32. if num := numReg1.FindString(tmpstr); num != "" {
  33. f_num := util.Float64All(num)
  34. if strings.Contains(tmpstr, "万") {
  35. f, _ := strconv.ParseFloat(strconv.FormatFloat(f_num*10000.0, 'f', 4, 64), 64)
  36. return f
  37. } else if strings.Contains(tmpstr, "亿") {
  38. f, _ := strconv.ParseFloat(strconv.FormatFloat(f_num*10000.0*10000.0, 'f', 4, 64), 64)
  39. return f
  40. } else {
  41. f, _ := strconv.ParseFloat(strconv.FormatFloat(f_num, 'f', 4, 64), 64)
  42. return f
  43. }
  44. }
  45. return 0.0
  46. }