1

I would like to convert Bytes into TB and I found this calculator:

function byte_umrechner($bytes) {
    if ($bytes > pow(2, 10)) {
        if ($bytes > pow(2, 40)) {
            $size = number_format(($bytes / pow(2, 40)), 4, ',', '');
            $size. = " TB";
            return $size;
        }

        if ($bytes > pow(2, 30)) {
            $size = number_format(($bytes / pow(2, 30)), 2, ',', '');
            $size. = " GB";
            return $size;
        }
        elseif($bytes > pow(2, 20)) {
            $size = number_format(($bytes / pow(2, 20)), 2, ',', '');
            $size. = " MB";
            return $size;
        } else {
            $size = number_format(($bytes / pow(2, 10)), 2, ',', '');
            $size. = " KB";
            return $size;
        }
    } else {
        $size = (string) $bytes.
        " Bytes";
        return $size;
    }
}

echo byte_umrechner(1099511627776);

My result in this case is 1024,00 GB

But it should actually show 1TB because 1099511627776 Bytes actually are exactly 1TB.

Jan.J
  • 3,050
  • 1
  • 23
  • 33
peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 6
    Shouldn't you use `>=` for the case comparisons then? Also `1<<40` is often considered more readable than `pow()`. – mario Aug 11 '15 at 11:45
  • 1
    [this](http://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes) seems a better logic..Have a look! –  Aug 11 '15 at 12:16
  • Thank you codeSun, but with your suggested code I get the result 1099511627776 TB – peace_love Aug 11 '15 at 12:46
  • @Mario: Thank you, `>=` gave me the result 1,000 TB – peace_love Aug 11 '15 at 12:49

0 Answers0