From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Converting an Integer into Human Readable String
Date: Fri, 08 Apr 2011 21:12:35 +0200 [thread overview]
Message-ID: <87ipuopnfg.fsf@gmail.com> (raw)
In-Reply-To: 83pqowu5y2.fsf@gnu.org
Eli Zaretskii <eliz@gnu.org> writes:
>> From: "Pascal J. Bourguignon" <pjb@informatimago.com>
>> Date: Fri, 08 Apr 2011 13:41:30 +0200
>>
>> Instead, you could have had some fun, and implement yourself the
>> required function. Well, now you've got only what you merit, here I
>> had all the fun, you can have the function:
>>
>> (format-human-readable-big-number 123456789012
>> *normal-format*
>> *exceptional-format*
>> "B" t :binary)
>> --> " 114.978 GiB"
>>
>>
>> Now, you may call this function in the places of interest in emacs.
>
> Wow! I've just installed in Emacs a much more modest variant
> (reproduced below for those who don't live on the bleeding edge).
>
> (defun file-size-human-readable (file-size &optional flavor)
> "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."
> (let ((power (if (or (null flavor) (eq flavor 'iec))
> 1024.0
> 1000.0))
> (post-fixes
> ;; none, kilo, mega, giga, tera, peta, exa, zetta, yotta
> (list "" "k" "M" "G" "T" "P" "E" "Z" "Y")))
> (while (and (>= file-size power) (cdr post-fixes))
> (setq file-size (/ file-size power)
> post-fixes (cdr post-fixes)))
> (format "%.0f%s%s" file-size
> (if (and (eq flavor 'iec) (string= (car post-fixes) "k"))
> "K"
> (car post-fixes))
> (if (eq flavor 'iec) "iB" ""))))
The result you obtain is not what is expected (i.e same than
ls -lh)
--8<---------------cut here---------------start------------->8---
ls -l .VirtualBox/Machines/LoseDows.vdi
-rw------- 1 thierry thierry 7141892608 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
ls -lh .VirtualBox/Machines/LoseDows.vdi
-rw------- 1 thierry thierry 6,7G 2011-04-07 08:51 .VirtualBox/Machines/LoseDows.vdi
(file-size-human-readable 7141892608.0)
"7G"
(anything-ff-human-size 7141892608.0)
"6.7G"
--8<---------------cut here---------------end--------------->8---
--
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997
next prev parent reply other threads:[~2011-04-08 19:12 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-07 10:02 Converting an Integer into Human Readable String Nordlöw
2011-04-07 11:34 ` Deniz Dogan
2011-04-07 12:04 ` Eli Zaretskii
2011-04-07 11:52 ` Eli Zaretskii
2011-04-07 12:51 ` Drew Adams
2011-04-07 17:43 ` Eli Zaretskii
2011-04-07 18:13 ` Drew Adams
2011-04-07 18:35 ` Eli Zaretskii
2011-04-07 18:51 ` Drew Adams
2011-04-07 20:10 ` Eli Zaretskii
2011-04-07 18:44 ` Eli Zaretskii
2011-04-07 19:01 ` Thierry Volpiatto
[not found] ` <mailman.17.1302204476.27822.help-gnu-emacs@gnu.org>
2011-04-08 6:35 ` Klaus Straubinger
2011-04-08 7:07 ` Thierry Volpiatto
2011-04-08 8:29 ` Eli Zaretskii
2011-04-08 9:38 ` Thierry Volpiatto
2011-04-08 11:41 ` Pascal J. Bourguignon
2011-04-08 15:19 ` Eli Zaretskii
2011-04-08 19:12 ` Thierry Volpiatto [this message]
2011-04-08 21:36 ` Eli Zaretskii
2011-04-09 5:24 ` Thierry Volpiatto
2011-04-09 8:46 ` Eli Zaretskii
[not found] ` <mailman.1.1302338931.20547.help-gnu-emacs@gnu.org>
2011-04-09 9:33 ` Pascal J. Bourguignon
2011-04-09 12:13 ` Eli Zaretskii
[not found] ` <mailman.0.1302351322.29796.help-gnu-emacs@gnu.org>
2011-04-11 15:31 ` Ted Zlatanov
2011-04-11 16:57 ` Eli Zaretskii
2011-05-12 19:30 ` Ted Zlatanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ipuopnfg.fsf@gmail.com \
--to=thierry.volpiatto@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.