import transaction from wechat pay billing file

This commit is contained in:
MaysWind
2024-10-14 01:18:17 +08:00
parent 44fe7778b6
commit b9b501edfa
11 changed files with 481 additions and 3 deletions
+16
View File
@@ -3,9 +3,14 @@ package utils
import (
"crypto/rand"
"math/big"
"regexp"
"strings"
)
var (
numberPattern = regexp.MustCompile("(-?\\d+)(\\.\\d+)?")
)
// GetRandomInteger returns a random number, the max parameter represents upper limit
func GetRandomInteger(max int) (int, error) {
result, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
@@ -17,6 +22,17 @@ func GetRandomInteger(max int) (int, error) {
return int(result.Int64()), nil
}
// ParseFirstConsecutiveNumber returns the first consecutive number in the specified string
func ParseFirstConsecutiveNumber(str string) (string, bool) {
result := numberPattern.FindAllString(str, 1)
if len(result) > 0 {
return result[0], true
} else {
return "", false
}
}
// TrimTrailingZerosInDecimal returns a textual number without trailing zeros in decimal
func TrimTrailingZerosInDecimal(num string) string {
if len(num) < 1 {
+30
View File
@@ -6,6 +6,36 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParseFirstConsecutiveNumber(t *testing.T) {
expectedValue := "¥123.45"
actualValue, success := ParseFirstConsecutiveNumber(expectedValue)
assert.True(t, success)
assert.Equal(t, "123.45", actualValue)
expectedValue = "$-123.45"
actualValue, success = ParseFirstConsecutiveNumber(expectedValue)
assert.True(t, success)
assert.Equal(t, "-123.45", actualValue)
expectedValue = "$0.12$123.45"
actualValue, success = ParseFirstConsecutiveNumber(expectedValue)
assert.True(t, success)
assert.Equal(t, "0.12", actualValue)
expectedValue = "$.12"
actualValue, success = ParseFirstConsecutiveNumber(expectedValue)
assert.True(t, success)
assert.Equal(t, "12", actualValue)
expectedValue = ""
actualValue, success = ParseFirstConsecutiveNumber(expectedValue)
assert.False(t, success)
expectedValue = "xff"
actualValue, success = ParseFirstConsecutiveNumber(expectedValue)
assert.False(t, success)
}
func TestTrimTrailingZerosInDecimal(t *testing.T) {
expectedValue := "123.45"
actualValue := TrimTrailingZerosInDecimal("123.45000000000")