remove trailing zeros
This commit is contained in:
parent
617efceec2
commit
1886921e17
7
cfg.go
7
cfg.go
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue