mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 12:30:49 +01:00
Implement new initialization function - NewFromFormattedString (#184)
* Implement new initialization function - NewFromFormattedString * Redefine docstring
This commit is contained in:
parent
5a22b86679
commit
ebbf8bbe36
2 changed files with 60 additions and 0 deletions
25
decimal.go
25
decimal.go
|
@ -22,6 +22,7 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
@ -176,6 +177,30 @@ func NewFromString(value string) (Decimal, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// NewFromFormattedString returns a new Decimal from a formatted string representation.
|
||||
// The second argument - replRegexp, is a regular expression that is used to find characters that should be
|
||||
// removed from given decimal string representation. All matched characters will be replaced with an empty string.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// r := regexp.MustCompile("[$,]")
|
||||
// d1, err := NewFromFormattedString("$5,125.99", r)
|
||||
//
|
||||
// r2 := regexp.MustCompile("[_]")
|
||||
// d2, err := NewFromFormattedString("1_000_000", r2)
|
||||
//
|
||||
// r3 := regexp.MustCompile("[USD\\s]")
|
||||
// d3, err := NewFromFormattedString("5000 USD", r3)
|
||||
//
|
||||
func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (Decimal, error) {
|
||||
parsedValue := replRegexp.ReplaceAllString(value, "")
|
||||
d, err := NewFromString(parsedValue)
|
||||
if err != nil {
|
||||
return Decimal{}, err
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// RequireFromString returns a new Decimal from a string representation
|
||||
// or panics if NewFromString would have returned an error.
|
||||
//
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"math/big"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -240,6 +241,35 @@ func TestNewFromString(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNewFromFormattedString(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
Formatted string
|
||||
Expected string
|
||||
ReplRegex *regexp.Regexp
|
||||
}{
|
||||
{"$10.99", "10.99", regexp.MustCompile("[$]")},
|
||||
{"$ 12.1", "12.1", regexp.MustCompile("[$\\s]")},
|
||||
{"$61,690.99", "61690.99", regexp.MustCompile("[$,]")},
|
||||
{"1_000_000.00", "1000000.00", regexp.MustCompile("[_]")},
|
||||
{"41,410.00", "41410.00", regexp.MustCompile("[,]")},
|
||||
{"5200 USD", "5200", regexp.MustCompile("[USD\\s]")},
|
||||
} {
|
||||
dFormatted, err := NewFromFormattedString(testCase.Formatted, testCase.ReplRegex)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dExact, err := NewFromString(testCase.Expected)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !dFormatted.Equal(dExact) {
|
||||
t.Errorf("expect %s, got %s", dExact, dFormatted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
for _, x := range testTable {
|
||||
if x.inexact == "" || x.inexact == "-" {
|
||||
|
@ -294,6 +324,11 @@ func TestNewFromStringErrs(t *testing.T) {
|
|||
"123.456Easdf",
|
||||
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
|
||||
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
|
||||
"512.99 USD",
|
||||
"$99.99",
|
||||
"51,850.00",
|
||||
"20_000_000.00",
|
||||
"$20_000_000.00",
|
||||
}
|
||||
|
||||
for _, s := range tests {
|
||||
|
|
Loading…
Reference in a new issue