An open API service indexing awesome lists of open source software.

https://github.com/linkdata/bytecount

Human readable byte count using KB, MB, GB etc
https://github.com/linkdata/bytecount

formatter go golang human-readable human-readable-numbers

Last synced: 7 months ago
JSON representation

Human readable byte count using KB, MB, GB etc

Awesome Lists containing this project

README

          

[![build](https://github.com/linkdata/bytecount/actions/workflows/go.yml/badge.svg)](https://github.com/linkdata/bytecount/actions/workflows/go.yml)
[![coverage](https://github.com/linkdata/bytecount/blob/coverage/main/badge.svg)](https://htmlpreview.github.io/?https://github.com/linkdata/bytecount/blob/coverage/main/report.html)
[![goreport](https://goreportcard.com/badge/github.com/linkdata/bytecount)](https://goreportcard.com/report/github.com/linkdata/bytecount)
[![Docs](https://godoc.org/github.com/linkdata/bytecount?status.svg)](https://godoc.org/github.com/linkdata/bytecount)

# bytecount

Go pretty printer for byte counts.

Only depends on the standard library.

## Usage

`go get github.com/linkdata/bytecount`

When printing byte counts using the `fmt` package, pass the values using `bytecount.N(n)`.

You may pass width and precision if you wish. The default is to keep the output to
a maximum of six characters while still showing as much precision as possible.
The highest byte count printable is 268435440QB (Quetta-bytes, 10³⁰),
exceeding this will print `+InfQB`.

If the formatting verb is `d` (e.g. `"%d"`), the divisor is 1000 rather than 1024.

If the formatting verb is `b` (e.g. `"%b"`), the value is multiplied by 8 and the
unit suffix is changed from `B` (bytes) to `b` (bits).

If the `"#"` flag is given, no unit suffix is written.

If the `" "` flag is given, a space is written between the digits and the suffix.

## Example

```go
import (
"fmt"

"github.com/linkdata/bytecount"
)

func main() {
fmt.Print(bytecount.N(53667551))
// Output:
// 51.2MB
}
```