Skip to content

Commit

Permalink
pull out changes into new public function
Browse files Browse the repository at this point in the history
  • Loading branch information
yungtrizzle authored and jackc committed Dec 9, 2023
1 parent 12582a0 commit 20bf953
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
27 changes: 20 additions & 7 deletions pgtype/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ func (n Numeric) Int64Value() (Int8, error) {
return Int8{Int64: bi.Int64(), Valid: true}, nil
}

func (n *Numeric) ScanScientific(src string) error {
if !strings.ContainsAny("eE", src) {
return scanPlanTextAnyToNumericScanner{}.Scan([]byte(src), n)
}

if bigF, ok := new(big.Float).SetString(string(src)); ok {
smallF, _ := bigF.Float64()
src = strconv.FormatFloat(smallF, 'f', -1, 64)
}

num, exp, err := parseNumericString(src)
if err != nil {
return err
}

*n = Numeric{Int: num, Exp: exp, Valid: true}

return nil
}

func (n *Numeric) toBigInt() (*big.Int, error) {
if n.Exp == 0 {
return n.Int, nil
Expand Down Expand Up @@ -759,13 +779,6 @@ 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, 64))
}
}

num, exp, err := parseNumericString(string(src))
if err != nil {
return err
Expand Down
12 changes: 0 additions & 12 deletions pgtype/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,6 @@ func TestNumericUnmarshalJSON(t *testing.T) {
src: []byte("1234.56789"),
wantErr: false,
},
{
name: "float: 1e10",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1), Exp: 10},
src: []byte("1e10"),
wantErr: false,
},
{
name: "float: 1.000101231014e10",
want: &pgtype.Numeric{Valid: true, Int: big.NewInt(1000101231014), Exp: -2},
src: []byte("1.000101231014e10"),
wantErr: false,
},
{
name: "invalid value",
want: &pgtype.Numeric{},
Expand Down

0 comments on commit 20bf953

Please sign in to comment.