diff --git a/numeric.go b/numeric.go index fc8e1789..f4ddb789 100644 --- a/numeric.go +++ b/numeric.go @@ -64,12 +64,18 @@ func (dst *Numeric) Set(src interface{}) error { switch value := src.(type) { case float32: + if math.IsNaN(float64(value)) { + return nil + } num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64)) if err != nil { return err } *dst = Numeric{Int: num, Exp: exp, Status: Present} case float64: + if math.IsNaN(value) { + return nil + } num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64)) if err != nil { return err diff --git a/numeric_test.go b/numeric_test.go index 263c78b6..3b099b55 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -210,6 +210,8 @@ func TestNumericSet(t *testing.T) { {source: float64(1234), result: &pgtype.Numeric{Int: big.NewInt(1234), Exp: 0, Status: pgtype.Present}}, {source: float64(12345678900), result: &pgtype.Numeric{Int: big.NewInt(123456789), Exp: 2, Status: pgtype.Present}}, {source: float64(12345.678901), result: &pgtype.Numeric{Int: big.NewInt(12345678901), Exp: -6, Status: pgtype.Present}}, + {source: math.NaN(), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Undefined}}, + {source: float32(math.NaN()), result: &pgtype.Numeric{Int: nil, Exp: 0, Status: pgtype.Undefined}}, } for i, tt := range successfulTests {