File Attributes conversion

Sam Collett sam_collett at lycos.co.uk
Fri Mar 8 07:06:42 EST 2002


How would you remove trailing 0's?
e.g.
Instead of 12.20kb it would be 12.2kb
23kb instead of 23.00 etc

Chris Barker <Chris.Barker at noaa.gov> wrote in message news:<3C87A28B.549D9B1C at noaa.gov>...
> Sam Collett wrote:
> > 
> > > Actually, I usually use a few more cases so that I always get two
> > > significant digits:  1.2k, 23k, 840k, 2.4M, etc.
> > How would you do that then (maybe even to 2 decimal places, e.g. 24.52kb)?
> 
> You make sure the result of your division is a Float (Ah, how I long for
> the "new division" to be standard!) and you use %f instead of %d
> 
> #!/usr/bin/env python
> 
> 
> def ShowSize(raw):
>     if raw < 3000:
>         return "%db" % raw
>     elif raw < 2000000:
>         return "%.2fkb" % (raw / 1000.)
>     elif raw < 2000000000L:
>         return "%.2fMb" % (raw / 1000000.)
>     else:
>         return "%.2fGb" % (raw / 1000000000.)
> 
> 
> print ShowSize(2567L)
> print ShowSize(2567000L)
> print ShowSize(256700000L)
> print ShowSize(2567000000L)
> print ShowSize(256700000000L)
> 
> And here are the results:
> 
> >>> execfile("junk.py")
> 2567b
> 2.57M
> 256.70M
> 2.57G
> 256.70G
> 
> -- 
> Christopher Barker, Ph.D.
> Oceanographer
>                                     		
> NOAA/OR&R/HAZMAT         (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
> 
> Chris.Barker at noaa.gov



More information about the Python-list mailing list