Skip to content

Commit

Permalink
support scientific notation big floats
Browse files Browse the repository at this point in the history
  • Loading branch information
yungtrizzle authored and jackc committed Dec 9, 2023
1 parent 913e4c8 commit 95b2f85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 36 deletions.
8 changes: 8 additions & 0 deletions pgtype/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,14 @@ func (scanPlanTextAnyToNumericScanner) Scan(src []byte, dst any) error {
return scanner.ScanNumeric(Numeric{InfinityModifier: NegativeInfinity, Valid: true})
}

if strings.ContainsAny(string(src), "eE") {
if bigF, ok := new(big.Float).SetString(string(src)); ok {
smallF, _ := bigF.Float64()

src = []byte(strconv.FormatFloat(smallF, 'f', -1, int(bigF.Prec())))
}
}

num, exp, err := parseNumericString(string(src))
if err != nil {
return err
Expand Down
84 changes: 48 additions & 36 deletions pgtype/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,46 +241,58 @@ func TestNumericUnmarshalJSON(t *testing.T) {
src []byte
wantErr bool
}{
// {
// name: "null",
// want: &pgtype.Numeric{},
// src: []byte(`null`),
// wantErr: false,
// },
// {
// name: "NaN",
// want: &pgtype.Numeric{Valid: true, NaN: true},
// src: []byte(`"NaN"`),
// wantErr: false,
// },
// {
// name: "0",
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(0)},
// src: []byte("0"),
// wantErr: false,
// },
// {
// name: "1",
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1)},
// src: []byte("1"),
// wantErr: false,
// },
// {
// name: "-1",
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(-1)},
// src: []byte("-1"),
// wantErr: false,
// },
// {
// name: "bigInt",
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 30},
// src: []byte("1000000000000000000000000000000"),
// wantErr: false,
// },
// {
// name: "float: 1234.56789",
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(123456789), Exp: -5},
// src: []byte("1234.56789"),
// wantErr: false,
// },
{
name: "null",
want: &pgtype.Numeric{},
src: []byte(`null`),
wantErr: false,
},
{
name: "NaN",
want: &pgtype.Numeric{Valid: true, NaN: true},
src: []byte(`"NaN"`),
wantErr: false,
},
{
name: "0",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(0)},
src: []byte("0"),
wantErr: false,
},
{
name: "1",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1)},
src: []byte("1"),
wantErr: false,
},
{
name: "-1",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(-1)},
src: []byte("-1"),
wantErr: false,
},
{
name: "bigInt",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 30},
src: []byte("1000000000000000000000000000000"),
name: "float: 1e10",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 10},
src: []byte("1e10"),
wantErr: false,
},
{
name: "float: 1234.56789",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(123456789), Exp: -5},
src: []byte("1234.56789"),
name: "float: 1.000101231014e10",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1000101231014), Exp: -2},
src: []byte("1.000101231014e10"),
wantErr: false,
},
{
Expand Down

0 comments on commit 95b2f85

Please sign in to comment.