diff --git a/decimal/decimal.go b/decimal/decimal.go index 0accca95..a57f1d05 100644 --- a/decimal/decimal.go +++ b/decimal/decimal.go @@ -82,9 +82,16 @@ func atoi64(s string) (bool, int64, error) { // error if integer parsing fails. func NewFromString(s string) (Decimal, error) { if whole, frac, split := strings.Cut(s, "."); split { - neg, w, err := atoi64(whole) - if err != nil { - return Zero, err + var neg bool + var w int64 + if whole == "-" { + neg = true + } else if whole != "" { + var err error + neg, w, err = atoi64(whole) + if err != nil { + return Zero, err + } } // overflow @@ -115,7 +122,7 @@ func NewFromString(s string) (Decimal, error) { if neg { f = -f } - return Decimal(w + f), err + return Decimal(w + f), nil } else { _, i, err := atoi64(s) if i > parseMax || i < parseMin { diff --git a/decimal/decimal_test.go b/decimal/decimal_test.go index 5b6d6954..5320bfaf 100644 --- a/decimal/decimal_test.go +++ b/decimal/decimal_test.go @@ -341,6 +341,16 @@ var testParseCases = []testCase{ `atoi failed`, "(123 * 6)", }, + { + "missingwhole", + "0.50", + ".50", + }, + { + "negmissingwhole", + "-0.50", + "-.50", + }, } func TestStringParse(t *testing.T) {