Skip to content

Commit

Permalink
Fixed an error when printing certain numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Dec 5, 2020
1 parent f3e3245 commit dc24107
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/number/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func (n Number) String() string {
str := strconv.Itoa(int(n / scale))
remainder := n.Abs() % scale
if remainder != 0 {
str += "." + strconv.Itoa(int(remainder))
deci := strconv.Itoa(int(remainder))
str += "." + strings.Repeat("0", 3-len(deci)) + deci
str = strings.TrimRight(str, "0")
}
return str
}
Expand Down
35 changes: 33 additions & 2 deletions pkg/number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,26 @@ func TestFromString(t *testing.T) {
teststr: "-5.5",
want: FromFloat64(-5.5),
},
{
teststr: "649.94",
want: FromFloat64(649.94),
},
{
teststr: "649.940",
want: FromFloat64(649.94),
},
{
teststr: "649.094",
want: FromFloat64(649.094),
},
}
for _, tt := range tests {
got, err := FromString(tt.teststr)
if err != nil && !tt.wanterr {
t.Errorf("FromString() error = %v", err)
continue
}
if !reflect.DeepEqual(got, tt.want) {
if got != tt.want {
t.Errorf("FromString() = %v, want %v", got, tt.want)
}
}
Expand Down Expand Up @@ -137,6 +149,20 @@ func TestFromFloat64(t *testing.T) {
},
MustFromString("-123.789"),
},
{
"test5",
args{
123.780,
},
MustFromString("123.78"),
},
{
"test5",
args{
123.078,
},
MustFromString("123.078"),
},
}
for _, tt := range tests {
if got := FromFloat64(tt.args.in); !reflect.DeepEqual(got, tt.want) {
Expand Down Expand Up @@ -198,7 +224,7 @@ func TestNumber_String(t *testing.T) {
{
"test3",
MustFromString("123.1"),
"123.100",
"123.1",
},
{
"test4",
Expand All @@ -210,6 +236,11 @@ func TestNumber_String(t *testing.T) {
MustFromString("-123.789"),
"-123.789",
},
{
"test6",
MustFromString("-123.089"),
"-123.089",
},
}
for _, tt := range tests {
if got := tt.n.String(); got != tt.want {
Expand Down

0 comments on commit dc24107

Please sign in to comment.