Browse Source

remove trailing zeros

master v1.0
Lars Hoogestraat 3 years ago
parent
commit
1886921e17
  1. 7
      cfg.go
  2. 3
      cfg_test.go
  3. 3
      go.mod

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 {

3
cfg_test.go

@ -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

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