import transaction from wechat pay billing file
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user