# How to print human readable byte counts

**URL:** <https://discourse.julialang.org/t/how-to-print-human-readable-byte-counts/31877>\
**Category:** Data\
**Created:** [December 4, 2019, 10:50pm UTC](https://discourse.julialang.org/t/how-to-print-human-readable-byte-counts/31877 "2019-12-04T22:50:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleisher](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mleisher](https://discourse.julialang.org/u/mleisher)\
**Post date:** [December 4, 2019, 10:50pm UTC](https://discourse.julialang.org/t/how-to-print-human-readable-byte-counts/31877/1 "2019-12-04T22:50:21Z")

</div>

I came across [Java: Formatting byte size to human readable format | Programming.Guide](https://programming.guide/java/formatting-byte-size-to-human-readable-format.html) this morning, and having a few minutes, adapted the Java code to Julia:

[https://www.cs.nmsu.edu/~mleisher/julia/hrbc.jl](https://www.cs.nmsu.edu/~mleisher/julia/hrbc.jl)

Being relatively new to Julia, this code is a little clumsy, but I’m sure someone can improve it. If anyone wants to adapt it to support Zetta and Yotta byte values, please do.

---

<div class="post-metadata">

**Author:** ![peteristhegreat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peteristhegreat/32/8220_2.png) [@peteristhegreat](https://discourse.julialang.org/u/peteristhegreat)\
**Post date:** [February 2, 2021, 5:02pm UTC](https://discourse.julialang.org/t/how-to-print-human-readable-byte-counts/31877/2 "2021-02-02T17:02:04Z")

</div>

Here is a shorter version that is converted from a javascript stackoverflow answer

[https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript](https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript)

```julia
function format_bytes(bytes, decimals = 2)
    bytes == 0 && return "0 Bytes"
    k = 1024
    dm = decimals < 0 ? 0 : decimals
    sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
    i = convert(Int, floor(log(bytes) / log(k)))
    return string(round((bytes / ^(k, i)), digits=dm)) * " " * sizes[i+1];
end

```

Tested on Julia 1.3.1.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [February 2, 2021, 5:19pm UTC](https://discourse.julialang.org/t/how-to-print-human-readable-byte-counts/31877/3 "2021-02-02T17:19:37Z")

</div>

There is also this (used by `@time` etc, not exported):

```julia
julia> Base.format_bytes(13422449238)
"12.501 GiB"

```
