mirror of https://github.com/jackc/pgx.git
added NaN support to Numeric.Set
parent
9d847241cb
commit
3cbb81631a
|
@ -64,12 +64,18 @@ func (dst *Numeric) Set(src interface{}) error {
|
||||||
|
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case float32:
|
case float32:
|
||||||
|
if math.IsNaN(float64(value)) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64))
|
num, exp, err := parseNumericString(strconv.FormatFloat(float64(value), 'f', -1, 64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*dst = Numeric{Int: num, Exp: exp, Status: Present}
|
*dst = Numeric{Int: num, Exp: exp, Status: Present}
|
||||||
case float64:
|
case float64:
|
||||||
|
if math.IsNaN(value) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64))
|
num, exp, err := parseNumericString(strconv.FormatFloat(value, 'f', -1, 64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -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(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(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: 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 {
|
for i, tt := range successfulTests {
|
||||||
|
|
Loading…
Reference in New Issue