Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add digits keyword argument to format_bytes #55495

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ Format a given number of bytes into a human-readable string.

# Arguments
- `bytes`: The number of bytes to format.
- `binary=true`: If `true`, formats the bytes in binary units (powers of 1024). If `false`, uses decimal units (powers of 1000).
- `binary=true`: If `true`, format the bytes in binary units (powers of 1024). If `false`, use decimal units (powers of 1000).
- `digits=3`: Round to the specified number of digits after the decimal place.

# Returns
`String`: A human-readable string representation of the bytes, formatted in either binary or decimal units based on the `binary` argument.
Expand All @@ -154,18 +155,21 @@ julia> Base.format_bytes(1024)
julia> Base.format_bytes(10000)
"9.766 KiB"

julia> Base.format_bytes(10000; digits=1)
"9.8 KiB"

julia> Base.format_bytes(10000, binary=false)
"10.000 kB"
```
"""
function format_bytes(bytes; binary=true) # also used by InteractiveUtils
function format_bytes(bytes; binary=true, digits::Integer=3) # also used by InteractiveUtils
units = binary ? _mem_units : _cnt_units
factor = binary ? 1024 : 1000
bytes, mb = prettyprint_getunits(bytes, length(units), Int64(factor))
if mb == 1
return string(Int(bytes), " ", _mem_units[mb], bytes==1 ? "" : "s")
else
return string(Ryu.writefixed(Float64(bytes), 3), binary ? " $(units[mb])" : "$(units[mb])B")
return string(Ryu.writefixed(Float64(bytes), digits), binary ? " $(units[mb])" : "$(units[mb])B")
end
end

Expand Down