Explorar el Código

feat: 金额处理相关函数

kvii hace 1 mes
padre
commit
3a6b12db7b
Se han modificado 2 ficheros con 196 adiciones y 0 borrados
  1. 55 0
      common/amount.go
  2. 141 0
      common/amount_test.go

+ 55 - 0
common/amount.go

@@ -0,0 +1,55 @@
+package common
+
+import (
+	"math"
+	"strconv"
+	"strings"
+)
+
+// MoneyNegation 金额取反 负负得正
+func MoneyNegation(s string) string {
+	switch {
+	case s == "":
+		return s
+	case s[0] == '-':
+		return s[1:]
+	default:
+		return "-" + s
+	}
+}
+
+// IsAmountEqual 判断两个金额字符串是否相等
+func IsAmountEqual(amount1, amount2 string) bool {
+	// 规范化金额字符串
+	normalizedAmount1 := normalizeAmount(amount1)
+	normalizedAmount2 := normalizeAmount(amount2)
+	if normalizedAmount1 == "" {
+		normalizedAmount1 = "0"
+	}
+	if normalizedAmount2 == "" {
+		normalizedAmount2 = "0"
+	}
+
+	// 转换为浮点数进行比较
+	value1, err1 := strconv.ParseFloat(normalizedAmount1, 64)
+	value2, err2 := strconv.ParseFloat(normalizedAmount2, 64)
+
+	// 如果任意一个转换失败,则认为不相等
+	if err1 != nil || err2 != nil {
+		return false
+	}
+
+	// 比较浮点数
+	return math.Abs(value2-value1) < 0.0001
+}
+
+// normalizeAmount 规范化金额字符串
+func normalizeAmount(amount string) string {
+	// 去掉逗号和多余的空格
+	return strings.ReplaceAll(strings.TrimSpace(amount), ",", "")
+}
+
+// MoneyNormalize 金额标准化 "1,234.56" -> "1234.56"
+func MoneyNormalize(s string) string {
+	return strings.ReplaceAll(s, ",", "")
+}

+ 141 - 0
common/amount_test.go

@@ -0,0 +1,141 @@
+package common
+
+import (
+	"testing"
+)
+
+func TestMoneyNegation(t *testing.T) {
+	for _, s := range []struct {
+		amount string // 金额字符串
+		want   string // 期望结果
+	}{
+		{
+			amount: "",
+			want:   "",
+		},
+		{
+			amount: "-100",
+			want:   "100",
+		}, {
+			amount: "-1000.00",
+			want:   "1000.00",
+		},
+		{
+			amount: "-100.123",
+			want:   "100.123",
+		},
+		{
+			amount: "1000",
+			want:   "-1000",
+		},
+		{
+			amount: "1200.12",
+			want:   "-1200.12",
+		},
+		{
+			amount: "1200.00",
+			want:   "-1200.00",
+		},
+	} {
+		s1 := MoneyNegation(s.amount)
+		t.Logf("origin: %s, after: %s", s.amount, s1)
+		if s1 != s.want {
+			t.Errorf("Expected %s, got %s", s.want, s1)
+		}
+	}
+}
+
+func TestMoneyNormalize(t *testing.T) {
+	for _, tc := range []struct {
+		s    string
+		want string
+	}{
+		{
+			s:    "",
+			want: "",
+		},
+		{
+			s:    "8,200.00",
+			want: "8200.00",
+		},
+		{
+			s:    "123",
+			want: "123",
+		},
+		{
+			s:    "1,234.56",
+			want: "1234.56",
+		},
+	} {
+		r := MoneyNormalize(tc.s)
+		if r != tc.want {
+			t.Errorf("Expected %s, got %s", tc.want, r)
+		}
+	}
+}
+
+func TestIsAmountEqual(t *testing.T) {
+	for _, s := range []struct {
+		amount1 string // 金额字符串1
+		amount2 string // 金额字符串2
+		want    bool   // 期望结果
+	}{
+		{
+			amount1: "1000",
+			amount2: "1,000",
+			want:    true,
+		},
+		{
+			amount1: "1000.00",
+			amount2: "1000",
+			want:    true,
+		},
+		{
+			amount1: "-1000.00",
+			amount2: "-1,000.00",
+			want:    true,
+		},
+		{
+			amount1: "100.123",
+			amount2: "100.123",
+			want:    true,
+		},
+		{
+			amount1: "1000.00",
+			amount2: "1000.01",
+			want:    false,
+		},
+		{
+			amount1: "abc",
+			amount2: "1000",
+			want:    false,
+		},
+		{
+			amount1: "",
+			amount2: "0",
+			want:    true,
+		},
+		{
+			amount1: "0",
+			amount2: "0.00",
+			want:    true,
+		},
+		{
+			amount1: "1,200.12",
+			amount2: "1200.12",
+			want:    true,
+		},
+		{
+			amount1: "-0",
+			amount2: "0",
+			want:    true,
+		},
+	} {
+		// 执行测试
+		got := IsAmountEqual(s.amount1, s.amount2)
+		//t.Logf("amount1: %s, amount2: %s, expected: %t, got: %t", s.amount1, s.amount2, s.want, got)
+		if got != s.want {
+			t.Errorf("amount1: %s, amount2: %s: expected %t, but got %t", s.amount1, s.amount2, s.want, got)
+		}
+	}
+}