Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: GB, MB, KB and on..

by cez (Novice)
on Jul 29, 2004 at 23:30 UTC ( #378546=note: print w/replies, xml ) Need Help??


in reply to GB, MB, KB and on..

how about something like this?
sub prettyBytes { my $size = $_[0]; foreach ('b','kb','mb','gb','tb','pb') { return sprintf("%.2f",$size)."$_" if $size < 1024; $size /= 1024; } }

Replies are listed 'Best First'.
Re^2: GB, MB, KB and on..
by Joost (Canon) on Jul 29, 2004 at 23:40 UTC
      tell me i'm beautiful like you mean it.. :)

      i actually wrote that after i posed the question. its better than what i first tried.

      i'm still curious how other people would solve this, curiosity's sake!

Re^2: GB, MB, KB and on..
by glenn (Scribe) on Oct 21, 2013 at 13:12 UTC
    Fix if size is larger than PB. <code> sub prettyBytes { my $size = $_[0]; foreach ('b','kb','mb','gb','tb','pb') { last if $size < 1024; $size /= 1024; } return sprintf("%.2f",$size)."$_"; } <code>

      This does not work as expected as the value of $_ is undefined outside of the loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://378546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-28 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?