Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Divide by 1000? Are you selling disk drives? :-)

There is some debate about whether it's 1000 or 1024 but you should be consistent, whichever you choose. So if you have 1024B in a KB then you should have 1024KB in an MB and 1024 MB in a GB.

There is(was?) a proposal that since kilo and mega and giga already mean 10^3, 10^6 and 10^9 in all other fields that they should also mean that in computers and that we should say kibibyte, mebibyte and gigibyte (gibibyte?) when we're talking about the 1024 based versions. Needless to say this didn't catch on.

As for the original question, something like this would do the trick. I leave it as an excercise to fix the fact that 25 bytes comes out as 25.000B

my @sizes=qw( B KB MB GB TB PB); sub nice_size { my $size = shift; my $i = 0; while ($size > 1024) { $size = $size / 1024; $i++; } return sprintf("%.3f$sizes[$i]", $size); }

In reply to Re^2: GB, MB, KB and on.. by fergal
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found