5

I have a utility function that will display a filesize in an appropriate form like Windows Explorer does, i.e; convert it to nearest KB, MB, GB etc. I wanted to know if the code that i wrote is correct, and if it can be made simpler.

The function that i wrote is as follows :

public static function formatFileSize(bytes:int):String
    {
        if(bytes < 1024)
            return bytes + " bytes";
        else
        {
            bytes /= 1024;
            if(bytes < 1024)
                return bytes + " Kb";
            else
            {
                bytes /= 1024;
                if(bytes < 1024)
                    return bytes + " Mb";
                else
                {
                    bytes /= 1024;
                    if(bytes < 1024)
                        return bytes + " Gb";
                }
            }
        }
        return String(bytes);
    }

While it does the job for me at the moment, i feel it could be written in an even simpler way and maybe even optimized.

thanks in advance

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
midhunhk
  • 5,560
  • 7
  • 52
  • 83

2 Answers2

23

Here's a simpler way of doing it:

private var _levels:Array = ['bytes', 'Kb', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

private function bytesToString(bytes:Number):String
{
    var index:uint = Math.floor(Math.log(bytes)/Math.log(1024));
    return (bytes/Math.pow(1024, index)).toFixed(2) + this._levels[index];
}

I included it up to yottabytes for completeness :)

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • couldn't you just do something like `var level:int = int(bytes/1024)`, then print out with `return level + _levels[level];` with a catch for if it's less that 1Kb? – divillysausages Apr 27 '11 at 15:31
  • @divillysausages, er no. Have you even tried your suggestion? With your 'calculation', you'd only get the kylobytes. Filesizes are exponention (number / 1024 / 1024 / 1024 / ...etc) – J_A_X Apr 27 '11 at 15:41
  • thank you J_A_X for the answer. its perfectly what i was looking for. – midhunhk Apr 28 '11 at 06:28
3

@J_A_X has the best way to do this, however for the future, I suggest returning early when you find you have nested if...else...if statements like you have.

public static function formatFileSize(bytes:int):String
{
    if(bytes < 1024)
        return bytes + " bytes";

    bytes /= 1024;
    if(bytes < 1024)
        return bytes + " Kb";

    bytes /= 1024;
    if(bytes < 1024)
        return bytes + " Mb";

    bytes /= 1024;
    if(bytes < 1024)
        return bytes + " Gb";

    return String(bytes);
}
Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • thank you jeremy. can't think how i missed writing this way and instead ended up with a nested if. it did seem weird for me. – midhunhk Apr 28 '11 at 05:15
  • hey, do you think a recursive approach could perform better in this case? – midhunhk Apr 28 '11 at 06:29
  • @silverback: No, because then you would have to keep track of how far deep so you can accurately display if it's KB or GB, etc. – Jeremy Apr 28 '11 at 12:00