remove trailing zeros

This commit is contained in:
Lars Hoogestraat 2019-03-07 18:37:07 +01:00
parent 617efceec2
commit 1886921e17
3 changed files with 7 additions and 6 deletions

7
cfg.go
View File

@ -60,8 +60,11 @@ const (
var sizes = []string{"B", "KB", "MB", "GB", "TB"}
func (fs FileSize) HumanReadable() string {
exp := math.Round(math.Log(float64(fs)) / math.Log(1024))
exp := math.Floor(math.Log(float64(fs)) / math.Log(1024))
if exp > 4 {
exp = 4
}
s := sizes[int(exp)]
if exp == 0 {
@ -70,7 +73,7 @@ func (fs FileSize) HumanReadable() string {
val := float64(fs) / float64(math.Pow(1024, exp))
return fmt.Sprintf("%.2f %s", val, s)
return fmt.Sprintf("%s %s", strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", val), "0"), "."), s)
}
func (fs *FileSize) Unmarshal(value string) error {

View File

@ -160,7 +160,7 @@ func TestFileSizes(t *testing.T) {
}
if s.Byte != 5 {
t.Errorf("s.Byte expected to be %d bytes but was %d bytes", 5, s.Byte)
t.Errorf("s.Byte expected to be %d bytes but was %d", 5, s.Byte)
}
if s.Kilobyte != 1<<10 {
t.Errorf("s.Kilobyte expected to be %d bytes but was %d", 1<<10, s.Kilobyte)
@ -177,6 +177,7 @@ func TestFileSizes(t *testing.T) {
if s.Empty != 0 {
t.Errorf("s.Empty expected to be 0 but was %d", s.Empty)
}
}
func addConfig(path, filename string) ConfigFiles {

3
go.mod
View File

@ -1,3 +0,0 @@
module git.hoogi.eu/cfg
go 1.12