From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.help Subject: Re: Converting an Integer into Human Readable String Date: Fri, 08 Apr 2011 21:12:35 +0200 Message-ID: <87ipuopnfg.fsf@gmail.com> References: <4fa1ed35-b35b-4c8d-bfca-d7fae8da913b@glegroupsg2000goo.googlegroups.com> <87vcyphswl.fsf@kuiper.lan.informatimago.com> <83pqowu5y2.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1302290033 29664 80.91.229.12 (8 Apr 2011 19:13:53 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 8 Apr 2011 19:13:53 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Apr 08 21:13:43 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Q8H7n-0005kt-8W for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Apr 2011 21:13:43 +0200 Original-Received: from localhost ([127.0.0.1]:56591 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q8H7m-0006jE-DT for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Apr 2011 15:13:42 -0400 Original-Received: from [140.186.70.92] (port=36225 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q8H6y-0006iu-Vr for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 15:12:54 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q8H6x-0005g7-AD for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 15:12:52 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:48788) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q8H6w-0005fd-TX for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 15:12:51 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Q8H6t-000590-HG for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 21:12:47 +0200 Original-Received: from 221.77.197-77.rev.gaoland.net ([77.197.77.221]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 08 Apr 2011 21:12:47 +0200 Original-Received: from thierry.volpiatto by 221.77.197-77.rev.gaoland.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 08 Apr 2011 21:12:47 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 69 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 221.77.197-77.rev.gaoland.net User-Agent: Gnus/5.110016 (No Gnus v0.16) Emacs/23.3.50 (gnu/linux) Cancel-Lock: sha1:hO1sVAwt5revr74vT9Eax+3z4T4= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:80716 Archived-At: Eli Zaretskii writes: >> From: "Pascal J. Bourguignon" >> 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