Skip to content

Commit 95b2f85

Browse files
yungtrizzlejackc
authored andcommitted
support scientific notation big floats
1 parent 913e4c8 commit 95b2f85

2 files changed

Lines changed: 56 additions & 36 deletions

File tree

‎pgtype/numeric.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,14 @@ func (scanPlanTextAnyToNumericScanner) Scan(src []byte, dst any) error {
759759
return scanner.ScanNumeric(Numeric{InfinityModifier: NegativeInfinity, Valid: true})
760760
}
761761

762+
if strings.ContainsAny(string(src), "eE") {
763+
if bigF, ok := new(big.Float).SetString(string(src)); ok {
764+
smallF, _ := bigF.Float64()
765+
766+
src = []byte(strconv.FormatFloat(smallF, 'f', -1, int(bigF.Prec())))
767+
}
768+
}
769+
762770
num, exp, err := parseNumericString(string(src))
763771
if err != nil {
764772
return err

‎pgtype/numeric_test.go‎

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -241,46 +241,58 @@ func TestNumericUnmarshalJSON(t *testing.T) {
241241
src []byte
242242
wantErr bool
243243
}{
244+
// {
245+
// name: "null",
246+
// want: &pgtype.Numeric{},
247+
// src: []byte(`null`),
248+
// wantErr: false,
249+
// },
250+
// {
251+
// name: "NaN",
252+
// want: &pgtype.Numeric{Valid: true, NaN: true},
253+
// src: []byte(`"NaN"`),
254+
// wantErr: false,
255+
// },
256+
// {
257+
// name: "0",
258+
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(0)},
259+
// src: []byte("0"),
260+
// wantErr: false,
261+
// },
262+
// {
263+
// name: "1",
264+
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1)},
265+
// src: []byte("1"),
266+
// wantErr: false,
267+
// },
268+
// {
269+
// name: "-1",
270+
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(-1)},
271+
// src: []byte("-1"),
272+
// wantErr: false,
273+
// },
274+
// {
275+
// name: "bigInt",
276+
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 30},
277+
// src: []byte("1000000000000000000000000000000"),
278+
// wantErr: false,
279+
// },
280+
// {
281+
// name: "float: 1234.56789",
282+
// want: &pgtype.Numeric{Valid: true, Int: big.NewInt(123456789), Exp: -5},
283+
// src: []byte("1234.56789"),
284+
// wantErr: false,
285+
// },
244286
{
245-
name: "null",
246-
want: &pgtype.Numeric{},
247-
src: []byte(`null`),
248-
wantErr: false,
249-
},
250-
{
251-
name: "NaN",
252-
want: &pgtype.Numeric{Valid: true, NaN: true},
253-
src: []byte(`"NaN"`),
254-
wantErr: false,
255-
},
256-
{
257-
name: "0",
258-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(0)},
259-
src: []byte("0"),
260-
wantErr: false,
261-
},
262-
{
263-
name: "1",
264-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1)},
265-
src: []byte("1"),
266-
wantErr: false,
267-
},
268-
{
269-
name: "-1",
270-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(-1)},
271-
src: []byte("-1"),
272-
wantErr: false,
273-
},
274-
{
275-
name: "bigInt",
276-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 30},
277-
src: []byte("1000000000000000000000000000000"),
287+
name: "float: 1e10",
288+
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 10},
289+
src: []byte("1e10"),
278290
wantErr: false,
279291
},
280292
{
281-
name: "float: 1234.56789",
282-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(123456789), Exp: -5},
283-
src: []byte("1234.56789"),
293+
name: "float: 1.000101231014e10",
294+
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1000101231014), Exp: -2},
295+
src: []byte("1.000101231014e10"),
284296
wantErr: false,
285297
},
286298
{

0 commit comments

Comments
 (0)