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

Simply dividing two numbers is ugly to you? There are 1024 bytes in a KB. Divide by 1000 to get MB. Divide MB by 1000 to get GB.

edit...I mean divide by 1024

Replies are listed 'Best First'.
Re^2: GB, MB, KB and on..
by fergal (Chaplain) on Jul 29, 2004 at 23:33 UTC
    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); }
      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 :)