Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: GB, MB, KB and on..

by fergal (Chaplain)
on Jul 29, 2004 at 23:33 UTC ( [id://378548]=note: print w/replies, xml ) Need Help??


in reply to Re: GB, MB, KB and on..
in thread GB, MB, KB and on..

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); }

Replies are listed 'Best First'.
Re^3: GB, MB, KB and on.. (GiB, MiB, KiB and so on...)
by grinder (Bishop) on Jul 30, 2004 at 08:11 UTC
    There is some debate about whether it's 1000 or 1024

    The SI units for these two multiples have been standardised. See, for instance: Prefixes for binary multiples. The 1000 multiple gets to keep the K, M, G prefixes (in line with existing physical units like metres and ohms that we are used to representing in terms of thousands).

    For the 1024 multiple, the official term is binary: kilobinary, megabinary, yottabinary... abbreviated to KiB, MiB, GiB... YiB.

    The real push for making the distinction didn't come from the disk drive world. The impetus came from the telecommunications field, where bitrates are commonly expressed in terms of thousand bits per second, not 1024. This is where the most confusion arose: telecom engineers talking to software developers didn't agree on what K meant.

    - another intruder with the mooring of the heat of the Perl

      Those "official" terms only become actual terms if people are so foolish as to switch to them.

      Personally, I'm going to celebrate a leapsecond at the end of this year, even though the International Earth Rotation Service has declared there won't be one. So There! Y'all can catch up with me next June :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found