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.

43 lines
1.2 KiB

2 years ago
  1. package util
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. //四舍五入,默认保留2位小数位
  7. func Round(f float64) float64 {
  8. return RoundDecimal(f, 2)
  9. }
  10. func RoundDecimal(f float64, d int64) float64 {
  11. res, err := strconv.ParseFloat(fmt.Sprintf("%."+strconv.FormatInt(d, 32)+"f", f), 64)
  12. if err != nil {
  13. panic(err)
  14. }
  15. return res
  16. }
  17. //0.01412 --> 141.2 --> 141.2000 --> 141
  18. //1.01412 --> 10141.2 --> 10141.2000 --> 10141
  19. //9.99999 --> 99999.9 --> 99999.9000 --> 99999
  20. //0.014199999 --> 141.99999 --> 142.0000 --> 142
  21. //1.014199999 --> 10141.99999 --> 10142.0000 --> 10142
  22. //9.999999999 --> 99999.99999 --> 100000.0000 --> 100000
  23. //9.999999999 --> 9999999.999 --> 10000000.00 --> 10000000
  24. func GetIntPrice(v float64) int64 {
  25. //6位小数的价格,如:0.034875,四舍五入后向上取整转换为整数保存
  26. return int64(RoundDecimal(v*1000000, 0)) //价格存储的为4位小数的值
  27. }
  28. //1412 --> 0.1412 --> 0.1412
  29. //101412 --> 10.1412 --> 10.1412
  30. //999999 --> 99.9999 --> 99.9999
  31. //1419999 --> 141.9999 --> 141.9999
  32. //1014199999 --> 101419.9999 --> 101419.9999
  33. //999999999 --> 99999.9999 --> 99999.9999
  34. func GetFloatPrice(v int64) float64 {
  35. return float64(v) / 1000000.00
  36. }