lc_content_size_pretty (public)
lc_content_size_pretty [ -size size ] [ -precision precision ] \ [ -standard standard ]
Defined in packages/acs-lang/tcl/localization-procs.tcl
Transforms data size, provided in nonnegative bytes, to KB, MB... up to YB.
- Switches:
- -size (optional, defaults to
"0"
)- Size in bytes
- -precision (optional, defaults to
"1"
)- Numbers in the fractional part
- -standard (optional, defaults to
"decimal"
)- Standard to use for binary prefix. Three standards are supported currently by this proc: - decimal (default): SI (base-10, 1000 bytes = 1kB) - binary: IEC (base-2, 1024 bytes = 1KiB) - legacy: JEDEC (base-2, 1024 bytes = 1KB)
- Returns:
- Size in given standard units (e.g. '5.2 MB')
- Author:
- Héctor Romojaro
- Created:
- 2019-06-25
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- lang_test__lc_content_size_pretty
Source code: # # Localized byte/s # set bytes [lc_get "bytes"] set byte [lc_get "byte"] switch $standard { decimal { # # SI (base-10, 1000 bytes = 1KB) # set div 1000 set units [list $bytes kB MB GB TB PB EB ZB YB] } binary { # # IEC (base-2, 1024 bytes = 1KiB) # set div 1024 set units [list $bytes KiB MiB GiB TiB PiB EiB ZiB YiB] } legacy { # # JEDEC (base-2, 1024 bytes = 1KB) # set div 1024 set units [list $bytes KB MB GB TB PB EB ZB YB] } default { return "Unknown value $standard for -standard option" } } # # For empty size, we assume 0 # if {$size eq ""} { set size 0 } set len [string length $size] if {$size < $div} { # # 1 byte or n bytes # if {$size == 1} { set size_pretty [format "%s $byte" $size] } else { set size_pretty [format "%s $bytes" $size] } } else { # # > 1K # set unit [expr {($len - 1) / 3}] set size_pretty [format "%.${precision}f %s" [expr {$size / pow($div,$unit)}] [lindex $units $unit]] } # # Localize dot/comma just before return # set size_pretty "[lc_numeric [lindex $size_pretty 0]] [lindex $size_pretty 1]" return $size_prettyXQL Not present: PostgreSQL, Oracle Generic XQL file: packages/acs-lang/tcl/localization-procs.xql