统一金额分制重构
This commit is contained in:
+11
-14
@@ -11,26 +11,23 @@ func Round(value float64) float64 {
|
||||
return math.Round(value*10) / 10
|
||||
}
|
||||
|
||||
// ToCent 将角转换为分(整数),用于存储
|
||||
// 12.3角 -> 123分
|
||||
// 12.34角 -> 123分(自动舍入到角)
|
||||
func ToCent(jiao float64) int64 {
|
||||
return int64(math.Round(jiao * 10))
|
||||
// ToCent 将元转换为分(整数),用于存储。
|
||||
// 12.34 元 -> 1234 分
|
||||
func ToCent(yuan float64) int64 {
|
||||
return int64(math.Round(yuan * 100))
|
||||
}
|
||||
|
||||
// ToJiao 将分转换为角(0.1元精度),用于API响应
|
||||
// 123分 -> 12.3角
|
||||
// 1234分 -> 123.4角
|
||||
func ToJiao(cent int64) float64 {
|
||||
return float64(cent) / 10.0
|
||||
// ToDisplayYuan 将分转换为按角精度展示的元值。
|
||||
// 1234 分 -> 12.3 元
|
||||
func ToDisplayYuan(cent int64) float64 {
|
||||
return Round(float64(cent) / 100)
|
||||
}
|
||||
|
||||
// Format 格式化分为字符串(角精度,保留1位小数)
|
||||
// 123分 -> "12.3"
|
||||
// 1230分 -> "123.0"
|
||||
// 1234 分 -> "12.3"
|
||||
// 1230 分 -> "12.3"
|
||||
func Format(cent int64) string {
|
||||
jiao := ToJiao(cent)
|
||||
return fmt.Sprintf("%.1f", jiao)
|
||||
return fmt.Sprintf("%.1f", ToDisplayYuan(cent))
|
||||
}
|
||||
|
||||
// FormatWithSymbol 格式化分为带符号的字符串
|
||||
|
||||
@@ -5,46 +5,44 @@ import "testing"
|
||||
func TestToCent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jiao float64
|
||||
yuan float64
|
||||
want int64
|
||||
}{
|
||||
{"12.3角转123分", 12.3, 123},
|
||||
{"12.34角舍入到123分", 12.34, 123},
|
||||
{"12.36角舍入到124分", 12.36, 124},
|
||||
{"0.1角转1分", 0.1, 1},
|
||||
{"0.05角舍入到1分", 0.05, 1},
|
||||
{"0.04角舍入到0分", 0.04, 0},
|
||||
{"100角转1000分", 100.0, 1000},
|
||||
{"12.3元转1230分", 12.3, 1230},
|
||||
{"12.34元转1234分", 12.34, 1234},
|
||||
{"12.36元转1236分", 12.36, 1236},
|
||||
{"0.1元转10分", 0.1, 10},
|
||||
{"100元转10000分", 100.0, 10000},
|
||||
{"零值", 0.0, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ToCent(tt.jiao)
|
||||
got := ToCent(tt.yuan)
|
||||
if got != tt.want {
|
||||
t.Errorf("ToCent(%v) = %v, want %v", tt.jiao, got, tt.want)
|
||||
t.Errorf("ToCent(%v) = %v, want %v", tt.yuan, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToJiao(t *testing.T) {
|
||||
func TestToDisplayYuan(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cent int64
|
||||
want float64
|
||||
}{
|
||||
{"123分转12.3角", 123, 12.3},
|
||||
{"1234分转123.4角", 1234, 123.4},
|
||||
{"1分转0.1角", 1, 0.1},
|
||||
{"10分转1.0角", 10, 1.0},
|
||||
{"1000分转100.0角", 1000, 100.0},
|
||||
{"123分展示1.2元", 123, 1.2},
|
||||
{"1234分展示12.3元", 1234, 12.3},
|
||||
{"1236分展示12.4元", 1236, 12.4},
|
||||
{"10分展示0.1元", 10, 0.1},
|
||||
{"1000分展示10.0元", 1000, 10.0},
|
||||
{"零值", 0, 0.0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ToJiao(tt.cent)
|
||||
got := ToDisplayYuan(tt.cent)
|
||||
if got != tt.want {
|
||||
t.Errorf("ToJiao(%v) = %v, want %v", tt.cent, got, tt.want)
|
||||
t.Errorf("ToDisplayYuan(%v) = %v, want %v", tt.cent, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -56,11 +54,11 @@ func TestFormat(t *testing.T) {
|
||||
cent int64
|
||||
want string
|
||||
}{
|
||||
{"123分格式化", 123, "12.3"},
|
||||
{"1234分格式化", 1234, "123.4"},
|
||||
{"10分格式化", 10, "1.0"},
|
||||
{"1分格式化", 1, "0.1"},
|
||||
{"1000分格式化", 1000, "100.0"},
|
||||
{"123分格式化", 123, "1.2"},
|
||||
{"1234分格式化", 1234, "12.3"},
|
||||
{"1236分格式化", 1236, "12.4"},
|
||||
{"10分格式化", 10, "0.1"},
|
||||
{"1000分格式化", 1000, "10.0"},
|
||||
{"零值格式化", 0, "0.0"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -79,8 +77,8 @@ func TestFormatWithSymbol(t *testing.T) {
|
||||
cent int64
|
||||
want string
|
||||
}{
|
||||
{"123分带符号", 123, "¥12.3"},
|
||||
{"1000分带符号", 1000, "¥100.0"},
|
||||
{"123分带符号", 123, "¥1.2"},
|
||||
{"1000分带符号", 1000, "¥10.0"},
|
||||
{"零值带符号", 0, "¥0.0"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -138,22 +136,22 @@ func TestMinMax(t *testing.T) {
|
||||
// 测试双向转换的精度
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
originalJiao float64
|
||||
expectJiao float64 // 因为角精度,可能会有舍入
|
||||
name string
|
||||
originalYuan float64
|
||||
expectDisplay float64
|
||||
}{
|
||||
{"12.3角往返", 12.3, 12.3},
|
||||
{"12.34角往返(舍入)", 12.34, 12.3},
|
||||
{"100.0角往返", 100.0, 100.0},
|
||||
{"0.1角往返", 0.1, 0.1},
|
||||
{"12.3元往返", 12.3, 12.3},
|
||||
{"12.34元往返(展示到角)", 12.34, 12.3},
|
||||
{"100.0元往返", 100.0, 100.0},
|
||||
{"0.1元往返", 0.1, 0.1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cent := ToCent(tt.originalJiao)
|
||||
gotJiao := ToJiao(cent)
|
||||
if gotJiao != tt.expectJiao {
|
||||
t.Errorf("往返转换:%v角 -> %v分 -> %v角, 期望 %v角",
|
||||
tt.originalJiao, cent, gotJiao, tt.expectJiao)
|
||||
cent := ToCent(tt.originalYuan)
|
||||
got := ToDisplayYuan(cent)
|
||||
if got != tt.expectDisplay {
|
||||
t.Errorf("往返转换:%v元 -> %v分 -> %v元, 期望 %v元",
|
||||
tt.originalYuan, cent, got, tt.expectDisplay)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user