chore(init): 初始化解忧商行项目并完成 Phase 0 基座
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user