chore(init): 初始化解忧商行项目并完成 Phase 0 基座

This commit is contained in:
yml2213
2026-10-30 11:46:10 +08:00
commit 8d9481c251
689 changed files with 138703 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
package money
import (
"fmt"
"math"
)
// Round 将金额四舍五入到角(0.1元),统一全项目金额精度
// 例如:12.34 -> 12.3, 12.36 -> 12.4, 12.35 -> 12.4
func Round(value float64) float64 {
return math.Round(value*10) / 10
}
// ToCent 将元转换为分(整数),用于存储。
// 12.34 元 -> 1234 分
func ToCent(yuan float64) int64 {
return int64(math.Round(yuan * 100))
}
// ToDisplayYuan 将分转换为按角精度展示的元值。
// 1234 分 -> 12.3 元
func ToDisplayYuan(cent int64) float64 {
return Round(float64(cent) / 100)
}
// Format 格式化分为字符串(角精度,保留1位小数)
// 1234 分 -> "12.3"
// 1230 分 -> "12.3"
func Format(cent int64) string {
return fmt.Sprintf("%.1f", ToDisplayYuan(cent))
}
// FormatWithSymbol 格式化分为带符号的字符串
// 123分 -> "¥12.3"
func FormatWithSymbol(cent int64) string {
return "¥" + Format(cent)
}
// Min 返回两个金额中较小的值(角精度,兼容旧代码)
func Min(a, b float64) float64 {
if a < b {
return Round(a)
}
return Round(b)
}
// Max 返回两个金额中较大的值(角精度,兼容旧代码)
func Max(a, b float64) float64 {
if a > b {
return Round(a)
}
return Round(b)
}
// MinCent 返回两个分值中较小的值
func MinCent(a, b int64) int64 {
if a < b {
return a
}
return b
}
// MaxCent 返回两个分值中较大的值
func MaxCent(a, b int64) int64 {
if a > b {
return a
}
return b
}
+158
View File
@@ -0,0 +1,158 @@
package money
import "testing"
func TestToCent(t *testing.T) {
tests := []struct {
name string
yuan float64
want int64
}{
{"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.yuan)
if got != tt.want {
t.Errorf("ToCent(%v) = %v, want %v", tt.yuan, got, tt.want)
}
})
}
}
func TestToDisplayYuan(t *testing.T) {
tests := []struct {
name string
cent int64
want float64
}{
{"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 := ToDisplayYuan(tt.cent)
if got != tt.want {
t.Errorf("ToDisplayYuan(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestFormat(t *testing.T) {
tests := []struct {
name string
cent int64
want string
}{
{"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 {
t.Run(tt.name, func(t *testing.T) {
got := Format(tt.cent)
if got != tt.want {
t.Errorf("Format(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestFormatWithSymbol(t *testing.T) {
tests := []struct {
name string
cent int64
want string
}{
{"123分带符号", 123, "¥1.2"},
{"1000分带符号", 1000, "¥10.0"},
{"零值带符号", 0, "¥0.0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatWithSymbol(tt.cent)
if got != tt.want {
t.Errorf("FormatWithSymbol(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestRound(t *testing.T) {
tests := []struct {
name string
value float64
want float64
}{
{"12.34舍入到12.3", 12.34, 12.3},
{"12.36舍入到12.4", 12.36, 12.4},
{"12.35舍入到12.4", 12.35, 12.4},
{"0.05舍入到0.1", 0.05, 0.1},
{"0.04舍入到0.0", 0.04, 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Round(tt.value)
if got != tt.want {
t.Errorf("Round(%v) = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestMinMax(t *testing.T) {
t.Run("Min", func(t *testing.T) {
if got := Min(100, 200); got != 100 {
t.Errorf("Min(100, 200) = %v, want 100", got)
}
if got := Min(200, 100); got != 100 {
t.Errorf("Min(200, 100) = %v, want 100", got)
}
})
t.Run("Max", func(t *testing.T) {
if got := Max(100, 200); got != 200 {
t.Errorf("Max(100, 200) = %v, want 200", got)
}
if got := Max(200, 100); got != 200 {
t.Errorf("Max(200, 100) = %v, want 200", got)
}
})
}
// 测试双向转换的精度
func TestRoundTrip(t *testing.T) {
tests := []struct {
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},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cent := ToCent(tt.originalYuan)
got := ToDisplayYuan(cent)
if got != tt.expectDisplay {
t.Errorf("往返转换:%v元 -> %v分 -> %v元, 期望 %v元",
tt.originalYuan, cent, got, tt.expectDisplay)
}
})
}
}