Perlfunc: human_size()

Quicky function to get data size in human readable format.

sub human_size {
    my $size = shift();

    if ($size > 1024**4) {
        $size = sprintf("%.1fT",$size / 1024**4);
    } elsif ($size > 1024**3) {
        $size = sprintf("%.1fG",$size / 1024**3);
    } elsif ($size > 1024**2) {
        $size = sprintf("%.1fM",$size / 1024**2);
    } elsif ($size > 1024) {
        $size = sprintf("%.1fK",$size / 1024);
    } elsif ($size > 1) {
        $size = sprintf("%dB",$size);
    }

    return $size;
}


Note: Replies will be formatted with PHP Markdown Extra syntax.

Name: Email (Not Required):
 
Logged IP: 23.20.25.122
To prevent spam please submit by clicking the kitten: