diff --git a/numeric.go b/numeric.go index 5999eaf..b5caa41 100644 --- a/numeric.go +++ b/numeric.go @@ -448,8 +448,11 @@ func (src *Numeric) toFloat64() (float64, error) { return math.Inf(-1), nil } - if src.Exp == 1 { + switch { + case src.Exp == 0: return float64(src.Int.Int64()), nil + case src.Exp == 1: + return float64(src.Int.Int64() * 10), nil } buf := make([]byte, 0, 32) diff --git a/numeric_test.go b/numeric_test.go index 175d0f2..3e97405 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -385,6 +385,7 @@ func TestNumericEncodeDecodeBinary(t *testing.T) { 123, 0.000012345, 1.00002345, + 50.000000, math.NaN(), float32(math.NaN()), math.Inf(1), @@ -442,3 +443,22 @@ func TestNumericSmallNegativeValues(t *testing.T) { t.Fatalf("expected %s, got %s", "-0.000123", s) } } + +// https://github.com/jackc/pgtype/issues/210 +func TestNumericFloat64(t *testing.T) { + n := pgtype.Numeric{ + Int: big.NewInt(5), + Exp: 1, + Status: pgtype.Present, + } + + var f float64 + err := n.AssignTo(&f) + if err != nil { + t.Fatal(err) + } + + if f != 50.0 { + t.Fatalf("expected %s, got %f", "50", f) + } +}