package util import ( "fmt" "strconv" ) //四舍五入,默认保留2位小数位 func Round(f float64) float64 { return RoundDecimal(f, 2) } func RoundDecimal(f float64, d int64) float64 { res, err := strconv.ParseFloat(fmt.Sprintf("%."+strconv.FormatInt(d, 32)+"f", f), 64) if err != nil { panic(err) } return res } //0.01412 --> 141.2 --> 141.2000 --> 141 //1.01412 --> 10141.2 --> 10141.2000 --> 10141 //9.99999 --> 99999.9 --> 99999.9000 --> 99999 //0.014199999 --> 141.99999 --> 142.0000 --> 142 //1.014199999 --> 10141.99999 --> 10142.0000 --> 10142 //9.999999999 --> 99999.99999 --> 100000.0000 --> 100000 //9.999999999 --> 9999999.999 --> 10000000.00 --> 10000000 func GetIntPrice(v float64) int64 { //6位小数的价格,如:0.034875,四舍五入后向上取整转换为整数保存 return int64(RoundDecimal(v*1000000, 0)) //价格存储的为4位小数的值 } //1412 --> 0.1412 --> 0.1412 //101412 --> 10.1412 --> 10.1412 //999999 --> 99.9999 --> 99.9999 //1419999 --> 141.9999 --> 141.9999 //1014199999 --> 101419.9999 --> 101419.9999 //999999999 --> 99999.9999 --> 99999.9999 func GetFloatPrice(v int64) float64 { return float64(v) / 1000000.00 }