Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Value ¶
type Value float32
Value is a byte count expressed as a float32, formatted via Value.Format as a short human-readable string with an SI-style scale prefix and a unit suffix (`B` for bytes, `b` for bits). The zero value formats as "0B".
func N ¶
func N[T numeric](n T) Value
N converts any numeric value to a Value suitable for passing to the fmt package. Conversion goes through float32, so very large or very precise integers may lose precision.
Example ¶
package main
import (
"fmt"
"math"
"github.com/linkdata/bytecount"
)
func main() {
var n int64
n = 64
for i := 0; i < 10; i++ {
n++
n *= 7
fmt.Printf("%v %v\n", n, bytecount.N(n))
}
fmt.Printf("%v %v\n", math.MaxFloat32, bytecount.N(math.MaxFloat32))
}
Output: 455 455B 3192 3.12kB 22351 21.8kB 156464 153kB 1095255 1.04MB 7666792 7.31MB 53667551 51.2MB 375672864 358MB 2629710055 2.45GB 18407970392 17.1GB 3.4028234663852886e+38 268435440QB
func (Value) Format ¶
Format implements fmt.Formatter. It supports the verbs `v`, `d`, and `b`:
- `v`: bytes with a 1024 divisor and `B` suffix.
- `d`: bytes with a 1000 (SI) divisor and `B` suffix.
- `b`: bits (value multiplied by 8) with a 1024 divisor and `b` suffix.
The `#` flag omits the unit suffix. The ` ` (space) flag inserts a space between the digits and the scale/unit. The `+`, `-`, `0`, width, and precision flags behave as in the standard fmt package. Unsupported verbs produce the usual `%!verb(type=value)` error string.