mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
fix some small issues with scientific notation, add more tests
This commit is contained in:
parent
f402c8cd87
commit
9995dc92ee
2 changed files with 7 additions and 3 deletions
|
@ -80,6 +80,7 @@ func New(value int64, exp int32) Decimal {
|
||||||
// d2, err := NewFromString(".0001")
|
// d2, err := NewFromString(".0001")
|
||||||
//
|
//
|
||||||
func NewFromString(value string) (Decimal, error) {
|
func NewFromString(value string) (Decimal, error) {
|
||||||
|
originalInput := value
|
||||||
var intString string
|
var intString string
|
||||||
var exp int64
|
var exp int64
|
||||||
|
|
||||||
|
@ -88,8 +89,7 @@ func NewFromString(value string) (Decimal, error) {
|
||||||
if eIndex != -1 {
|
if eIndex != -1 {
|
||||||
expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32)
|
expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rerr := err.(*strconv.NumError)
|
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange {
|
||||||
if rerr.Err == strconv.ErrRange {
|
|
||||||
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
|
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
|
||||||
}
|
}
|
||||||
return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value)
|
return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value)
|
||||||
|
@ -119,7 +119,7 @@ func NewFromString(value string) (Decimal, error) {
|
||||||
|
|
||||||
if exp < math.MinInt32 || exp > math.MaxInt32 {
|
if exp < math.MinInt32 || exp > math.MaxInt32 {
|
||||||
// NOTE(vadim): I doubt a string could realistically be this long
|
// NOTE(vadim): I doubt a string could realistically be this long
|
||||||
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
|
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", originalInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Decimal{
|
return Decimal{
|
||||||
|
|
|
@ -132,10 +132,14 @@ func TestNewFromStringErrs(t *testing.T) {
|
||||||
"1e9e",
|
"1e9e",
|
||||||
"1ee9",
|
"1ee9",
|
||||||
"1ee",
|
"1ee",
|
||||||
|
"1eE",
|
||||||
|
"1e-",
|
||||||
|
"1e-.",
|
||||||
"1e1.2",
|
"1e1.2",
|
||||||
"123.456e1.3",
|
"123.456e1.3",
|
||||||
"1e-1.2",
|
"1e-1.2",
|
||||||
"123.456e-1.3",
|
"123.456e-1.3",
|
||||||
|
"123.456Easdf",
|
||||||
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
|
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
|
||||||
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
|
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue