horse: fix TenThousandths formatting
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package horse
|
package horse
|
||||||
|
|
||||||
import "strconv"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
type SkillID int32
|
type SkillID int32
|
||||||
|
|
||||||
@@ -9,11 +13,14 @@ type TenThousandths int32
|
|||||||
func (x TenThousandths) String() string {
|
func (x TenThousandths) String() string {
|
||||||
b := make([]byte, 0, 12)
|
b := make([]byte, 0, 12)
|
||||||
b = strconv.AppendInt(b, int64(x/1e4), 10)
|
b = strconv.AppendInt(b, int64(x/1e4), 10)
|
||||||
b = append(b, '.')
|
if x%1e4 != 0 {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
x = -x
|
x = -x
|
||||||
|
}
|
||||||
|
b = append(b, '.')
|
||||||
|
b = fmt.Appendf(b, "%04d", x%1e4)
|
||||||
|
b = bytes.TrimRight(b, "0")
|
||||||
}
|
}
|
||||||
b = strconv.AppendInt(b, int64(x%1e4), 10)
|
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,3 +32,25 @@ func TestSkillStrings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTenThousandthsString(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
cases := []struct {
|
||||||
|
val horse.TenThousandths
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{0, "0"},
|
||||||
|
{10000, "1"},
|
||||||
|
{-10000, "-1"},
|
||||||
|
{15000, "1.5"},
|
||||||
|
{-15000, "-1.5"},
|
||||||
|
{10001, "1.0001"},
|
||||||
|
{5000000, "500"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got := c.val.String()
|
||||||
|
if got != c.want {
|
||||||
|
t.Errorf("%d: want %q, got %q", c.val, c.want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user