Skip to content

Commit

Permalink
fix negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Mar 9, 2023
1 parent 155d1a5 commit 8270530
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion chain/types/percent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import (
type Percent int64

func (p Percent) String() string {
return fmt.Sprintf(`%d.%d`, p/100, p%100)
abs := p
sign := ""
if abs < 0 {
abs = -abs
sign = "-"
}
return fmt.Sprintf(`%s%d.%d`, sign, abs/100, abs%100)
}

func (p Percent) MarshalJSON() ([]byte, error) {
Expand Down
3 changes: 3 additions & 0 deletions chain/types/percent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func TestPercent(t *testing.T) {
{100, "1.0"},
{111, "1.11"},
{12, "0.12"},
{-12, "-0.12"},
{1012, "10.12"},
{-1012, "-10.12"},
{0, "0.0"},
} {
tc := tc
t.Run(fmt.Sprintf("%d <> %s", tc.p, tc.s), func(t *testing.T) {
Expand Down

0 comments on commit 8270530

Please sign in to comment.