From b2d095670f49c5a091a524e1d5c17a848c2f3b11 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Thu, 15 Aug 2024 11:18:28 +0200 Subject: [PATCH 1/2] add `sigdigits` keyword argument to `format_bytes` --- base/timing.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/base/timing.jl b/base/timing.jl index 80ebb74abee26..20e31f67fec63 100644 --- a/base/timing.jl +++ b/base/timing.jl @@ -142,6 +142,7 @@ 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). +- `sigdigits=3`: The number of significant digits to display when formatting the bytes. # Returns `String`: A human-readable string representation of the bytes, formatted in either binary or decimal units based on the `binary` argument. @@ -154,18 +155,21 @@ julia> Base.format_bytes(1024) julia> Base.format_bytes(10000) "9.766 KiB" +julia> Base.format_bytes(10000; sigdigits=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, sigdigits::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), sigdigits), binary ? " $(units[mb])" : "$(units[mb])B") end end From bcaad8751d5172a6b2e4ceb45af8ffa229da91e0 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Thu, 15 Aug 2024 14:11:30 +0200 Subject: [PATCH 2/2] Update timing.jl --- base/timing.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/base/timing.jl b/base/timing.jl index 20e31f67fec63..3cb5b071e1bed 100644 --- a/base/timing.jl +++ b/base/timing.jl @@ -141,8 +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). -- `sigdigits=3`: The number of significant digits to display when formatting the bytes. +- `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. @@ -155,21 +155,21 @@ julia> Base.format_bytes(1024) julia> Base.format_bytes(10000) "9.766 KiB" -julia> Base.format_bytes(10000; sigdigits=1) +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, sigdigits::Integer=3) # 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), sigdigits), binary ? " $(units[mb])" : "$(units[mb])B") + return string(Ryu.writefixed(Float64(bytes), digits), binary ? " $(units[mb])" : "$(units[mb])B") end end