formatting fix and test added

This commit is contained in:
sqshq 2019-05-23 23:51:37 -04:00
parent 4ec36459d8
commit eb2f9949b6
2 changed files with 30 additions and 1 deletions

View File

@ -71,7 +71,9 @@ func formatTrailingDigits(value string, scale int) string {
formatted = value[:i+scale] formatted = value[:i+scale]
} }
return strings.TrimRight(formatted, "0.") formatted = strings.TrimRight(formatted, "0")
return strings.TrimRight(formatted, ".")
} }
return value return value

View File

@ -0,0 +1,27 @@
package util
import "testing"
func TestFormatValue(t *testing.T) {
type args struct {
value float64
scale int
}
tests := []struct {
name string
args args
want string
}{
{"should format float value with scale = 1", args{94.123, 1}, "94.1"},
{"should format float value with scale = 0", args{94.123, 0}, "94"},
{"should format float value with trailing zeros", args{94.100, 5}, "94.1"},
{"should format float value with radix char and trailing zeros", args{9423000.00123, 2}, "9,423,000"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatValue(tt.args.value, tt.args.scale); got != tt.want {
t.Errorf("FormatValue() = %v, want %v", got, tt.want)
}
})
}
}