Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the function I use.</p> <pre><code>(defconst number-to-string-approx-suffixes '("k" "M" "G" "T" "P" "E" "Z" "Y")) (defun number-to-string-approx-suffix (n &amp;optional binary) "Return an approximate decimal representation of NUMBER as a string, followed by a multiplier suffix (k, M, G, T, P, E, Z, Y). The representation is at most 5 characters long for numbers between 0 and 10^19-5*10^16. Uses a minus sign if negative. NUMBER may be an integer or a floating point number. If the optional argument BINARY is non-nil, use 1024 instead of 1000 as the base multiplier." (if (zerop n) "0" (let ((sign "") (b (if binary 1024 1000)) (suffix "") (bigger-suffixes number-to-string-approx-suffixes)) (if (&lt; n 0) (setq n (- n) sign "-")) (while (and (&gt;= n 9999.5) (consp bigger-suffixes)) (setq n (/ n b) ; TODO: this is rounding down; nearest would be better suffix (car bigger-suffixes) bigger-suffixes (cdr bigger-suffixes))) (concat sign (if (integerp n) (int-to-string n) (number-to-string (floor n))) suffix)))) </code></pre> <p>I use it in the size column of the buffer menu.</p> <pre><code>(defvar Buffer-menu-buffer+size-shorten 'binary) (defadvice Buffer-menu-buffer+size (before Buffer-menu-shorten-size compile activate) "Shorten the size column in a buffer menu by using multiplier suffixes \(k, M, G, T\). This is done only if `Buffer-menu-buffer+size-shorten' is non-nil. If `Buffer-menu-buffer+size-shorten' is the symbol `binary', use binary multipliers (powers of 1024). Otherwise use decimal (powers of 1000) multipliers." (if Buffer-menu-buffer+size-shorten (let ((binary (eq Buffer-menu-buffer+size-shorten 'binary))) (save-match-data (if (string-match "^[0-9]+$" size) (setq size (number-to-string-approx-suffix (string-to-number size) binary))))))) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload