Skip to content

Commit

Permalink
fix: MarshalJSON serializes without decimal part using strconv.Format…
Browse files Browse the repository at this point in the history
…Int()
  • Loading branch information
trakhimenok committed May 9, 2024
1 parent 3709989 commit fe16035
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion decimal64p2.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func round(num float64) int {

// MarshalJSON marshals decimal to JSON
func (d Decimal64p2) MarshalJSON() ([]byte, error) {
return []byte(d.String()), nil
return []byte(strconv.FormatInt(int64(d), 10)), nil
}

// UnmarshalJSON deserializes JSON to decimal
Expand Down
8 changes: 4 additions & 4 deletions decimal64p2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ func TestDecimal64p2_MarshalJSON(t *testing.T) {
d1 = NewDecimal64p2(1, 23)
if s, err := json.Marshal(d1); err != nil {
t.Error(err)
} else if string(s) != d1.String() {
t.Errorf("Expected '%v', got: '%v'", d1.String(), string(s))
} else if expected := "123"; string(s) != expected {
t.Errorf("Expected '%v', got: '%v'", expected, string(s))
}

d1 = NewDecimal64p2(-1, -23)
if s, err := json.Marshal(d1); err != nil {
t.Error(err)
} else if string(s) != d1.String() {
t.Errorf("Expected '%v', got: '%v'", d1.String(), string(s))
} else if expected := "-123"; string(s) != expected {
t.Errorf("Expected '%v', got: '%v'", expected, string(s))
}
}

Expand Down

0 comments on commit fe16035

Please sign in to comment.