From: huang zhimin Date: 2007-10-04T19:20:04+09:00 Subject: Re: Human readable file sizes ------=_Part_6789_5621663.1191493197549 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I think this is what you want GIGA_SIZE =3D 1073741824.0 MEGA_SIZE =3D 1048576.0 KILO_SIZE =3D 1024.0 # Return the file size with a readable style. def readable_file_size(size, precision) case when size =3D=3D 1 : "1 Byte" when size < KILO_SIZE : "%d Bytes" % size when size < MEGA_SIZE : "%.#{precision}f KB" % (size / KILO_SIZE) when size < GIGA_SIZE : "%.#{precision}f MB" % (size / MEGA_SIZE) else "%.#{precision}f GB" % (size / GIGA_SIZE) end end On 10/4/07, Jes=FAs Gabriel y Gal=E1n wrote: > > Hi, > > I have a list of files and I need to produce a string containing the > size of each file, but in human readable format. With that I mean > something like what df -h does: make an approximation to the nearest > unit (B, KB, MB, GB, etc). Is this already done somewhere? This is > what I have now: > > Find.find(folder) do |file| > match =3D regexp.match(File.basename(file)); > if match > size =3D File.stat(file).size > puts size # would like a human readable format > end > end > > Thanks, > > Jesus. > > --=20 http://sourceforge.net/projects/mycodeline/ http://rubyforge.org/projects/ropenwiki/ ------=_Part_6789_5621663.1191493197549--