Skip to content

Commit 3c5b3c0

Browse files
authored
Add Base.format_bytes(; binary=false) option (#50572)
Add a `binary` keyword argument to `Base.format_bytes` that enables switching between the default units KiB, MiB, GiB, etc. and kB, MB, GB. I've wanted this feature multiple times before so I thought I should just make a PR. ```julia julia> Base.format_bytes(12345678) "11.774 MiB" julia> Base.format_bytes(12345678; binary=false) # with this PR "12.346 MB" ```
2 parents 3d944dd + f74bde8 commit 3c5b3c0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

base/timing.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@ function padded_nonzero_print(value, str, always_print = true)
128128
end
129129
end
130130

131-
function format_bytes(bytes) # also used by InteractiveUtils
132-
bytes, mb = prettyprint_getunits(bytes, length(_mem_units), Int64(1024))
131+
function format_bytes(bytes; binary=true) # also used by InteractiveUtils
132+
units = binary ? _mem_units : _cnt_units
133+
factor = binary ? 1024 : 1000
134+
bytes, mb = prettyprint_getunits(bytes, length(units), Int64(factor))
133135
if mb == 1
134-
return string(Int(bytes), " ", _mem_units[mb], bytes==1 ? "" : "s")
136+
return string(Int(bytes), " ", units[mb], bytes==1 ? "" : "s")
135137
else
136-
return string(Ryu.writefixed(Float64(bytes), 3), " ", _mem_units[mb])
138+
return string(Ryu.writefixed(Float64(bytes), 3), binary ? " $(units[mb])" : "$(units[mb])B")
137139
end
138140
end
139141

0 commit comments

Comments
 (0)