Skip to content

Commit 20bf953

Browse files
yungtrizzlejackc
authored andcommitted
pull out changes into new public function
1 parent 12582a0 commit 20bf953

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎pgtype/numeric.go‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ func (n Numeric) Int64Value() (Int8, error) {
119119
return Int8{Int64: bi.Int64(), Valid: true}, nil
120120
}
121121

122+
func (n *Numeric) ScanScientific(src string) error {
123+
if !strings.ContainsAny("eE", src) {
124+
return scanPlanTextAnyToNumericScanner{}.Scan([]byte(src), n)
125+
}
126+
127+
if bigF, ok := new(big.Float).SetString(string(src)); ok {
128+
smallF, _ := bigF.Float64()
129+
src = strconv.FormatFloat(smallF, 'f', -1, 64)
130+
}
131+
132+
num, exp, err := parseNumericString(src)
133+
if err != nil {
134+
return err
135+
}
136+
137+
*n = Numeric{Int: num, Exp: exp, Valid: true}
138+
139+
return nil
140+
}
141+
122142
func (n *Numeric) toBigInt() (*big.Int, error) {
123143
if n.Exp == 0 {
124144
return n.Int, nil
@@ -759,13 +779,6 @@ func (scanPlanTextAnyToNumericScanner) Scan(src []byte, dst any) error {
759779
return scanner.ScanNumeric(Numeric{InfinityModifier: NegativeInfinity, Valid: true})
760780
}
761781

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

‎pgtype/numeric_test.go‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,6 @@ func TestNumericUnmarshalJSON(t *testing.T) {
283283
src: []byte("1234.56789"),
284284
wantErr: false,
285285
},
286-
{
287-
name: "float: 1e10",
288-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 10},
289-
src: []byte("1e10"),
290-
wantErr: false,
291-
},
292-
{
293-
name: "float: 1.000101231014e10",
294-
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1000101231014), Exp: -2},
295-
src: []byte("1.000101231014e10"),
296-
wantErr: false,
297-
},
298286
{
299287
name: "invalid value",
300288
want: &pgtype.Numeric{},

0 commit comments

Comments
 (0)