Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created February 16, 2011 15:26
Show Gist options
  • Save sharifulin/829553 to your computer and use it in GitHub Desktop.
Save sharifulin/829553 to your computer and use it in GitHub Desktop.
Format Bytes
sub _format_byte {
my $self = shift;
my $byte = shift;
my $str;
for (reverse [ 1 => 'B' ], [ 1024 => 'KB' ], [ 1024 ** 2 => 'MB' ], [ 1024 ** 3 => 'GB' ], [ 1024 ** 4 => 'TB' ]) {
my $temp = $byte / $_->[0];
$str = sprintf "%0.2f $_->[1]", $temp;
$str =~ s/\.?0+ / /;
last if $temp >= 0.5;
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment