Blog Details

  • Home
  • PHP
  • Given a size in KB, try to return a more readable MB, GB or TB answer string
ptitle-particle1

Given a size in KB, try to return a more readable MB, GB or TB answer string


function readable_size_for_kb($mykb)
{
        if ( $mykb > 1073741824 )
        {
                return sprintf("%5.1f TB",$mykb / 1024 / 1024/1024);
        }
        elseif ( $mykb > 1048576 )
        {
                return sprintf("%5.1f GB",$mykb / 1024 / 1024);
        }
        elseif ( $mykb > 1024 )
        {
                return sprintf("%5.1f MB",$mykb / 1024);
        }
        else
        {
                return sprintf("%5.1f KB",$mykb);
        }

}

Cart

No products in the basket.