import re


def _size_to_bytes(size):
	"""
	Parse a string with a size into a number of bytes. I.e. parses "10m", "10MB", "10 M" and other variations into the
	number of bytes in ten megabytes. Floating-point numbers are rounded to the nearest byte.

	:type size: ``str``
	:param size: The size to parse, given as a string with byte unit. No byte unit is assumed to be in bytes. Scientific
	notation is not allowed; must be an integer or real number followed by a case-insensitive byte unit (e.g. as "k" or
	"KB" for kilobyte, "g" or "Gb" for gigabyte, or a similar convention). Positive/negative sign in front of number is
	allowed.
	:rtype: ``long``
	:return: The number of bytes represented by the given string.
	"""
	units = 'KMGTPEZY'  # note that position of letter is same as power - 1
	match = re.search(r'^\s*([-+]?\s*[0-9]*\.?[0-9]*)\s*([' + units + r']?\s*B?\s*S?)\s*', size, re.IGNORECASE)
	if match is None or match.group(1) == '':
		raise ValueError("size string not in proper format 'number [kmgtpezy]': " + size)
	mem_size = float(re.sub(r'\s*', '', match.group(1)))
	unit = re.sub(r'\s*', '', match.group(2)).upper()
	unit = re.sub(r'B?S?$', '', unit)  # remove trailing units symbol
	if unit == '':
		unit_pow = 0
	else:
		unit_pow = units.find(unit) + 1
	byte_size = int(round(mem_size * (1024 ** unit_pow)))
	return byte_size