统一金额分制重构
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 格式化分为带符号的字符串
|
||||
|
||||
Reference in New Issue
Block a user