统一金额分制重构

This commit is contained in:
yml
2026-06-09 19:04:11 +08:00
parent 87673288ef
commit 5cd3ead4bd
69 changed files with 886 additions and 1277 deletions
+11 -14
View File
@@ -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 格式化分为带符号的字符串