Function to format bytes in human readable format – MySQL

Just searched for a similar one, and could not find any, converted one such function which I had in hand, and is posted here Function to format bytes in human readable format, which was in php.

DELIMITER $$
DROP FUNCTION IF EXISTS `humansize`$$
DETERMINISTIC: CREATE FUNCTION `humansize` (ibytes bigint) RETURNS text DETERMINISTIC 
BEGIN
 if ibytes <= 1000 then
   return concat(ibytes, ' B');
 else
   if ibytes <= (1000* 1000) then
     return concat(ibytes / 1000, ' KB');
   else
     if ibytes < (1000* 1000 * 1000) then
       return concat(ibytes / (1000 * 1000), ' MB');
     else
       if ibytes < (1000* 1000 * 1000 * 1000) then
          return concat(ibytes / (1000 * 1000 * 1000), ' GB');
       else
          return concat(ibytes / (1000 * 1000 * 1000 * 1000), ' TB');
       end if;
     end if;
   end if;
 end if;
END$$
DELIMITER ;