pull out changes into new public function

pull/1835/head
Eshton Robateau 2023-12-04 10:46:00 +08:00 committed by Jack Christensen
parent 12582a0fd4
commit 20bf953a17
2 changed files with 20 additions and 19 deletions

View File

@ -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
@ -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

View File

@ -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{},