diff --git a/numeric.go b/numeric.go index e6c58391..fc8e1789 100644 --- a/numeric.go +++ b/numeric.go @@ -291,19 +291,16 @@ func (dst *Numeric) toBigInt() (*big.Int, error) { } func (src *Numeric) toFloat64() (float64, error) { - f, err := strconv.ParseFloat(src.Int.String(), 64) + buf := make([]byte, 0, 32) + + buf = append(buf, src.Int.String()...) + buf = append(buf, 'e') + buf = append(buf, strconv.FormatInt(int64(src.Exp), 10)...) + + f, err := strconv.ParseFloat(string(buf), 64) if err != nil { return 0, err } - if src.Exp > 0 { - for i := 0; i < int(src.Exp); i++ { - f *= 10 - } - } else if src.Exp < 0 { - for i := 0; i > int(src.Exp); i-- { - f /= 10 - } - } return f, nil } diff --git a/numeric_test.go b/numeric_test.go index b925be83..263c78b6 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -266,6 +266,7 @@ func TestNumericAssignTo(t *testing.T) { {src: &pgtype.Numeric{Int: big.NewInt(42), Status: pgtype.Present}, dst: &_i8, expected: _int8(42)}, {src: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))}, {src: &pgtype.Numeric{Int: big.NewInt(0), Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))}, + {src: &pgtype.Numeric{Int: big.NewInt(1006), Exp: -2, Status: pgtype.Present}, dst: &f64, expected: float64(10.06)}, // https://github.com/jackc/pgtype/issues/27 } for i, tt := range simpleTests {