Your use of number_format converts 1000000 / 1000 to 1,000 (Kš
1,000 (Kš is a string that is converted to a number by your next call to number-format (for Mš
The string 1,000 (Kš gets evaluated to the number 1.
1 / 1000 = 0,001 (Mš => 0
Try
function convert_number_to_unit($number, $unit, $first_call = true) {
if ((int)$number === 0 || !$unit) return $number;
switch (strtoupper($unit)) {
case 'TB' :
$result = convert_number_to_unit($number, 'MB', false) >> 10;
break;
case 'MB' : // MEGABYTES
$result = convert_number_to_unit($number, 'KB', false) >> 10;
break;
case 'KB' : // KILOBYTES
$result = $number >> 10;
break;
}
if ($first_call)
return number_format($result).' '.strtoupper($unit);
else
return $result;
}