Function: file-size-human-readable
file-size-human-readable is a byte-compiled function defined in
files.el.gz.
Signature
(file-size-human-readable FILE-SIZE &optional FLAVOR SPACE UNIT)
Documentation
Produce a string showing FILE-SIZE in human-readable form.
Optional second argument FLAVOR controls the units and the display format:
If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
suffixes are "k", "M", "G", "T", etc.
If FLAVOR is si, each kilobyte is 1000 bytes and the produced suffixes
are "k", "M", "G", "T", etc.
If FLAVOR is iec, each kilobyte is 1024 bytes and the produced suffixes
are "KiB", "MiB", "GiB", "TiB", etc.
Optional third argument SPACE is a string put between the number and unit. It defaults to the empty string. We recommend a single space or non-breaking space, unless other constraints prohibit a space in that position.
Optional fourth argument UNIT is the unit to use. It defaults to "B"
when FLAVOR is iec and the empty string otherwise. We recommend "B"
in all cases, since that is the standard symbol for byte.
Probably introduced at or before Emacs version 24.1.
Aliases
url-pretty-length (obsolete since 24.4)
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
;; A handy function to display file sizes in human-readable form.
;; See https://en.wikipedia.org/wiki/Kibibyte for the reference.
(defun file-size-human-readable (file-size &optional flavor space unit)
"Produce a string showing FILE-SIZE in human-readable form.
Optional second argument FLAVOR controls the units and the display format:
If FLAVOR is nil or omitted, each kilobyte is 1024 bytes and the produced
suffixes are \"k\", \"M\", \"G\", \"T\", etc.
If FLAVOR is `si', each kilobyte is 1000 bytes and the produced suffixes
are \"k\", \"M\", \"G\", \"T\", etc.
If FLAVOR is `iec', each kilobyte is 1024 bytes and the produced suffixes
are \"KiB\", \"MiB\", \"GiB\", \"TiB\", etc.
Optional third argument SPACE is a string put between the number and unit.
It defaults to the empty string. We recommend a single space or
non-breaking space, unless other constraints prohibit a space in that
position.
Optional fourth argument UNIT is the unit to use. It defaults to \"B\"
when FLAVOR is `iec' and the empty string otherwise. We recommend \"B\"
in all cases, since that is the standard symbol for byte."
(let ((power (if (or (null flavor) (eq flavor 'iec))
1024.0
1000.0))
(prefixes '("" "k" "M" "G" "T" "P" "E" "Z" "Y" "R" "Q")))
(while (and (>= file-size power) (cdr prefixes))
(setq file-size (/ file-size power)
prefixes (cdr prefixes)))
(let* ((prefix (car prefixes))
(prefixed-unit (if (eq flavor 'iec)
(concat
(if (string= prefix "k") "K" prefix)
(if (string= prefix "") "" "i")
(or unit "B"))
(concat prefix unit))))
;; Mimic what GNU "ls -lh" does:
;; If the formatted size will have just one digit before the decimal...
(format (if (and (< file-size 10)
;; ...and its fractional part is not too small...
(>= (mod file-size 1.0) 0.05)
(< (mod file-size 1.0) 0.95))
;; ...then emit one digit after the decimal.
"%.1f%s%s"
"%.0f%s%s")
file-size
(if (string= prefixed-unit "") "" (or space ""))
prefixed-unit))))