def mem_format(memory, ratio=1.0):
    """Convert memory in G to memory requirement.

    Args:
        memory: Memory size in G.
        ratio: The percent that can be used.

    Returns:
        Formatted string of memory size if memory is valid, None otherwise.
    """
    try:
        memory = float(memory)
    except:
        return None
    else:
        return "%dM" % int(ratio * memory * 1024)