Converts the limit to the smallest possible number
(i.e. try "MB", then "kB", then "bytes").

This method should be kept in sync with Symfony\Component\Validator\Constraints\FileValidator::factorizeSizes().
421591
private function factorizeSizes($size, $limit)
    {
        $coef = self::MIB_BYTES;
        $coefFactor = self::KIB_BYTES;

        $limitAsString = (string) ($limit / $coef);

        // Restrict the limit to 2 decimals (without rounding! we
        // need the precise value)
        while (self::moreDecimalsThan($limitAsString, 2)) {
            $coef /= $coefFactor;
            $limitAsString = (string) ($limit / $coef);
        }

        // Convert size to the same measure, but round to 2 decimals
        $sizeAsString = (string) round($size / $coef, 2);

        // If the size and limit produce the same string output
        // (due to rounding), reduce the coefficient
        while ($sizeAsString === $limitAsString) {
            $coef /= $coefFactor;
            $limitAsString = (string) ($limit / $coef);
            $sizeAsString = (string) round($size / $coef, 2);
        }

        return [$limitAsString, self::$suffixes[$coef]];
    }
806656
public function parseFileSize($size)
    {
        $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
        $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
        if ($unit) {
            // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
            return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
        } else {
            return round($size);
        }
    }
597034
def filesize(num_bytes):
    """"""
    for prefix in '-KMGTEPZY':
        if num_bytes < 999.9:
            break
        num_bytes /= 1000.0
    if prefix == '-':
        return '{} B'.format(num_bytes)
    return '{:.3n} {}B'.format(num_bytes, prefix)
629971
public static String checkSize(byte[] value, int limitSize) {
		if (value != null) {
			if (value.length > limitSize) {
				return bytesToHex(value, limitSize-3) + "...";
			} else
				return bytesToHex(value, value.length);
		} else
			return null;

	}
367290
public function getFormattedSize(): string
    {
        $kiloBytes = $this->getSize() / 1024;
        if ($kiloBytes < 1024) {
            return round($kiloBytes, 2).' KB';
        }
        $megaBytes = $kiloBytes / 1024;
        if ($megaBytes < 1024) {
            return round($megaBytes, 1).' MB';
        }

        return round($megaBytes / 1024, 1).' GB';
    }
339416
public static function byteConvert($size, $precision = 2)
    {

        // Sanity check
        if (!is_numeric($size)) {
            return '?';
        }

        // Get the notation
        $notation = self::$settings['byte_notation'] == 1000 ? 1000 : 1024;

        // Fixes large disk size overflow issue
        // Found at http://www.php.net/manual/en/function.disk-free-space.php#81207
        $types = array('B', 'KB', 'MB', 'GB', 'TB');
        $types_i = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
        for ($i = 0; $size >= $notation && $i < (count($types) - 1); $size /= $notation, $i++);

        return(round($size, $precision).' '.($notation == 1000 ? $types[$i] : $types_i[$i]));
    }
579911
private function limitBytes($bytes)
    {
        if ($bytes === null) {
            return $this->content;
        } elseif (intval($bytes) < (self::BYTE_LIMIT * 0.046875)) {
            // less than 24 kilobytes (512 kilobytes * 0.046875)
            throw new \InvalidArgumentException('Byte limit is set dangerously low! Default value=' . self::BYTE_LIMIT);
        }
        return $this->content = mb_strcut($this->content, 0, intval($bytes));
    }
1038855
function convertOnes(num) {
  if (str(num).length > 1) {
    throw new Error("Must have at most 1 digit");
  }

  num = int(num);
  return mapOnes[num];
}
154309
public function human_filesize($bytes, $decimals = 0)
    {
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        $factor = floor((strlen($bytes) - 1) / 3);

        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)).@$size[$factor];
    }
394629
private function minLength()
	{
		$min = mb_strlen($this->value);
		if (!$ret = $min >= $this->options->min) {
			$this->error(2);
		}

		return $ret;
	}
1354359
public function getMaxOpenFilesLimit()
    {
        static $limit = null;

        if (null === $limit) {
            $process = new Process('ulimit -n');
            $process->run();

            if ($process->isSuccessful()) {
                $limit = (int) trim($process->getOutput());
            }

            unset($process);
        }

        return $limit;
    }
946511
private function LittleEndian2String($number, $minbytes = 1)
		# Author:     James Heinrich
		# Purpose:    BMP SUPPORT (SAVING)
		# Param in:
		# Param out:
		# Reference:
		# Notes:
		#
	{
		$intstring = '';
		while ($number > 0)
		{
			$intstring = $intstring . chr($number & 255);
			$number >>= 8;
		}

		return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
	}
1200140
static public function bytesToSize($bytes, $precision = 2)
    {
        $kilobyte = 1024;
        $megabyte = $kilobyte * 1024;
        $gigabyte = $megabyte * 1024;
        $terabyte = $gigabyte * 1024;

        if (($bytes >= 0) && ($bytes < $kilobyte)) {
            return $bytes . ' B';

        } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
            return round($bytes / $kilobyte, $precision) . ' KB';

        } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
            return round($bytes / $megabyte, $precision) . ' MB';

        } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
            return round($bytes / $gigabyte, $precision) . ' GB';

        } elseif ($bytes >= $terabyte) {
            return round($bytes / $terabyte, $precision) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
173480
def convert(self, value, param, ctx):  # pylint: disable=inconsistent-return-statements
        """"""
        matches = MEMORY_RE.match(value.lower())
        if matches is None:
            self.fail('%s is not a valid value for memory amount' % value, param, ctx)
        amount_str, unit = matches.groups()
        amount = int(amount_str)
        if unit in [None, 'm', 'mb']:
            # Assume the user intends gigabytes if they specify a number < 1024
            if amount < 1024:
                return amount * 1024
            else:
                if amount % 1024 != 0:
                    self.fail('%s is not an integer that is divisable by 1024' % value, param, ctx)
                return amount
        elif unit in ['g', 'gb']:
            return amount * 1024
623181
public static function sizeInBytes($value)
    {
        $value = trim($value);
        $last = strtolower($value[strlen($value)-1]);

        switch($last) {
            case 'g':
                $value *= 1024;
            case 'm':
                $value *= 1024;
            case 'k':
                $value *= 1024;
        }

        return (int) $value;
    }
1273987
private static function discardDecimalDigitsZero($amount, $minimumDecimalDigits = 0)
    {
        if (false !== strpos($amount, '.')) {
            while ('0' == substr($amount, -1)) {
                $amount = substr($amount, 0, -1);
            }

            if ('.' == substr($amount, -1)) {
                $amount = substr($amount, 0, -1);
            }
        }

        if ($minimumDecimalDigits > 0) {
            if (false === strpos($amount, '.')) {
                $amount .= '.';
            }

            $currentDecimalDigits = strlen(substr($amount, strpos($amount, '.') + 1));
            $minimumDecimalDigits -= $currentDecimalDigits;

            while ($minimumDecimalDigits > 0) {
                $amount .= '0';
                $minimumDecimalDigits--;
            }
        }

        return $amount;
    }
1107244
function byteSizeFormat(num, base, toFixed, noSpace) {
  if (typeof num !== 'number') {
    throw new TypeError('Expected a number');
  }

  var neg = num < 0 ? '-' : '';

  if (neg) {
    num = -num; // short Math.abs
  }
  if (num < 1) {
    return neg + num + ' B';
  }
  if (typeof base !== 'number' || base < 0) {
    base = 1024;
  }
  if (typeof num !== 'number' || toFixed < 0) {
    toFixed = 2;
  }

  var exponent = Math.min(Math.floor(Math.log(num) / Math.log(base)), units.length - 1);

  num = (num / Math.pow(base, exponent)).toFixed(toFixed);
  return neg + num + (noSpace ? '' : ' ') + units[exponent];
}