Commit 07e7e857 by 杨树贤

支持浮点数

parent a68834bb
Showing with 7 additions and 5 deletions
......@@ -154,7 +154,7 @@ type SpuExtra struct {
SpuId string `bson:"spu_id" json:"spu_id"`
Height string `json:"height" bson:"height"`
Weight string `json:"-" bson:"weight"`
TransformedWeight int `json:"weight"`
TransformedWeight float64 `json:"weight"`
Width string `json:"width" bson:"width"`
Length string `json:"length" bson:"length"`
}
......
......@@ -627,7 +627,7 @@ func DivFloat(first float64, second float64) float64 {
}
// 将重量单位统一转换为克,同时清洗数据
func ConvertToGrams(input string) (int, error) {
func ConvertToGrams(input string) (float64, error) {
// 清洗数据:去除左右空格,去除中间空格
input = strings.ReplaceAll(strings.TrimSpace(input), " ", "")
......@@ -654,11 +654,13 @@ func ConvertToGrams(input string) (int, error) {
// 根据单位返回对应的克数
switch unit {
case "kg", "千克":
return int(num * 1000), nil
return float64(num * 1000), nil
case "g", "克":
return int(num), nil
return float64(num), nil
case "t", "吨":
return int(num * 1000000), nil
return float64(num * 1000000), nil
case "mg", "毫克":
return float64(num / 1000), nil
default:
return 0, fmt.Errorf("未知的重量单位: %s", unit)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment