I was going to clean this up this recent code of mine (a lot! It's ugly!!) before posting, but that'd be awhile to find the time (this is from last weekend), and you asked, so... Also, I hadn't yet searched through the code posted on PM - I really wonder if this hasn't been done better elsewhere. (Not in any CPAN modules that I can find, though)

I really wanted to handle quite large values, like some that are up into terabytes in just six months, and so I felt I needed to be able to handle large integers ala Math::BigInt. And I wanted to be able to handle the "K is 1000" vs. the "K is 1024" people. And I wanted to be able to reliably fit the output into a limited number of columns. Thus the following contortions...

# Convert bigint to printable number using KB, MB, GB as needed. # Biggest string would be either 'NN.NXB' or 'NNNNXB' - 6 characters + long sub ToScaledFormat { my $bignum = Math::BigFloat->new( shift ); my $logbase = shift || 1000;; my $scaling_lower_limit = 10000; if( $bignum < $scaling_lower_limit ) { return int($bignum); } my @sfx = ( '', qw( KB MB GB TB PB EB ZB YB ) ); my $log1000 = $bignum->copy->blog($logbase); my $pow1000 = int($log1000); my $wow = exp( ($log1000 - $pow1000) * log($logbase) ); # It is possible below to round up to our logbase value, which loo +ks # wrong! If we would, instead bump up to next power set if( $wow >= ($logbase-0.5) ) { $wow /= $logbase; ++$pow1000; } # If we are not using a decimal base, we may need to show 1023KB # or such using four leading digits. Use an alternate format if( $wow >= 1000 ) { # If four significant digits return( sprintf("%.4g",$wow), $sfx[$pow1000] ); } else { return( sprintf("%.3g",$wow), $sfx[$pow1000] ); } }

--
I'm a pessimist about probabilities; I'm an optimist about possibilities.
Lewis Mumford


In reply to Re: GB, MB, KB and on.. by shenme
in thread GB, MB, KB and on.. by cez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.