For non-zero tenth fractions on vals < 10, show the tenths place.
diff --git a/bytes.go b/bytes.go
index e480c4e..e58eba2 100644
--- a/bytes.go
+++ b/bytes.go
@@ -38,7 +38,13 @@
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
- return fmt.Sprintf("%.0f%s", float64(s)/math.Pow(base, math.Floor(e)), suffix)
+ val := float64(s) / math.Pow(base, math.Floor(e))
+ f := "%.0f"
+ if val < 10 && int((val-math.Floor(val))*10) != 0 {
+ f = "%.1f"
+ }
+
+ return fmt.Sprintf(f+"%s", val, suffix)
}
diff --git a/bytes_test.go b/bytes_test.go
index 279eac6..c029ffb 100644
--- a/bytes_test.go
+++ b/bytes_test.go
@@ -85,3 +85,11 @@
// Overflows.
// assert(t, "bytes(1EB - 1P)", IBytes((KIByte*EIByte)-PiByte), "1023EB")
}
+
+func TestIHalf(t *testing.T) {
+ assert(t, "bytes(5.5GiB)", IBytes(5.5*GiByte), "5.5GiB")
+}
+
+func TestHalf(t *testing.T) {
+ assert(t, "bytes(5.5GB)", Bytes(5.5*GByte), "5.5GB")
+}