Skip to content

Commit

Permalink
Backport fix from pgx v5 b52ef1574d6eeb821df0ad58eba40f04dd4561dd
Browse files Browse the repository at this point in the history
fixes #210
  • Loading branch information
jackc committed Jan 13, 2024
1 parent cb24fcc commit fdca434
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,12 @@ func encodeNumericText(n Numeric, buf []byte) (newBuf []byte, err error) {
func (n Numeric) numberTextBytes() []byte {
intStr := n.Int.String()
buf := &bytes.Buffer{}

if len(intStr) > 0 && intStr[:1] == "-" {
intStr = intStr[1:]
buf.WriteByte('-')
}

exp := int(n.Exp)
if exp > 0 {
buf.WriteString(intStr)
Expand Down
19 changes: 19 additions & 0 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,22 @@ func TestNumericEncodeDecodeBinary(t *testing.T) {
}
}
}

// https://github.com/jackc/pgtype/issues/210
func TestNumericSmallNegativeValues(t *testing.T) {
n := pgtype.Numeric{}
err := n.Set("-0.000123")
if err != nil {
t.Fatal(err)
}

s := ""
err = n.AssignTo(&s)
if err != nil {
t.Fatal(err)
}

if s != "-0.000123" {
t.Fatalf("expected %s, got %s", "-0.000123", s)
}
}

0 comments on commit fdca434

Please sign in to comment.