ActionScript code to convert bytes to kb, mb, gb etc
Asked Answered
W

2

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

Workout answered 27/4, 2011 at 14:35 Comment(5)
if you use the 'curly bracket' button on your code it'll format it like code instead of as a quote.Landonlandor
you can use nested ? : operators instead of if(){}else{}As
@flextras : thank you for correcting that. i was not aware of that yet :)Workout
@As : that will end up getting more complex and cumbersome to manage.Workout
The abbreviations in your code should be KiB, MiB, and GiB. See here.Clein
K
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 :)

Kahlil answered 27/4, 2011 at 15:20 Comment(3)
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?Trimester
@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)Kahlil
thank you J_A_X for the answer. its perfectly what i was looking for.Workout
P
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);
}
Paschall answered 27/4, 2011 at 18:4 Comment(3)
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.Workout
hey, do you think a recursive approach could perform better in this case?Workout
@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.Paschall

© 2022 - 2024 — McMap. All rights reserved.