From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: Converting an Integer into Human Readable String Date: Sat, 09 Apr 2011 00:36:51 +0300 Message-ID: <83hba8togc.fsf@gnu.org> References: <4fa1ed35-b35b-4c8d-bfca-d7fae8da913b@glegroupsg2000goo.googlegroups.com> <87vcyphswl.fsf@kuiper.lan.informatimago.com> <83pqowu5y2.fsf@gnu.org> <87ipuopnfg.fsf@gmail.com> NNTP-Posting-Host: lo.gmane.org X-Trace: dough.gmane.org 1302298763 12598 80.91.229.12 (8 Apr 2011 21:39:23 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 8 Apr 2011 21:39:23 +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 23:39:19 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 1Q8JOh-0003rt-Ht for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Apr 2011 23:39:19 +0200 Original-Received: from localhost ([127.0.0.1]:58938 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q8JOg-0005sP-Sn for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Apr 2011 17:39:18 -0400 Original-Received: from [140.186.70.92] (port=45356 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q8JOJ-0005nB-Ns for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 17:38:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q8JOI-0006nY-AO for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 17:38:55 -0400 Original-Received: from mtaout22.012.net.il ([80.179.55.172]:58705) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q8JOH-0006n9-W8 for help-gnu-emacs@gnu.org; Fri, 08 Apr 2011 17:38:54 -0400 Original-Received: from conversion-daemon.a-mtaout22.012.net.il by a-mtaout22.012.net.il (HyperSendmail v2007.08) id <0LJC00F00S0ZE100@a-mtaout22.012.net.il> for help-gnu-emacs@gnu.org; Sat, 09 Apr 2011 00:38:52 +0300 (IDT) Original-Received: from HOME-C4E4A596F7 ([84.229.239.68]) by a-mtaout22.012.net.il (HyperSendmail v2007.08) with ESMTPA id <0LJC00ELAS4R0ML0@a-mtaout22.012.net.il> for help-gnu-emacs@gnu.org; Sat, 09 Apr 2011 00:38:52 +0300 (IDT) In-reply-to: <87ipuopnfg.fsf@gmail.com> X-012-Sender: halo1@inter.net.il X-detected-operating-system: by eggs.gnu.org: Solaris 10 (beta) X-Received-From: 80.179.55.172 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:80718 Archived-At: > From: Thierry Volpiatto > Date: Fri, 08 Apr 2011 21:12:35 +0200 > > 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" Well, that's what ls-lisp.el was always doing. But since you asked for it, here you go (will commit as soon as Savannah is fixed): (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 (if (> (mod file-size 1.0) 0.05) "%.1f%s%s" "%.0f%s%s") file-size (if (and (eq flavor 'iec) (string= (car post-fixes) "k")) "K" (car post-fixes)) (if (eq flavor 'iec) "iB" ""))))