import logging


def flops(nflops, suffix="FLOPS"):
    """Human readable flops.

    Args:
        nflops (float): The number of flops.
        suffix (str, optional): The suffix of flops in inputted number.

    Returns:
        String: The best readable FLOPS.
    """
    if nflops is None:
        return None

    try:
        suffixes = ["FLOPS", "KFLOPS", "MFLOPS", "GFLOPS", "TFLOPS", "PFLOPS"]
        index = 0

        if suffixes.index(suffix) >= 0:
            value = nflops * (1000.0**suffixes.index(suffix))
        else:
            value = nflops

        while value >= 1000.0 and index < len(suffixes) - 1:
            value /= 1000.0
            index += 1
        temp = "{:.2f}".format(value).rstrip("0").rstrip(".")
        return "{} {}".format(temp, suffixes[index])
    except IOError as err:
        logging.error(err)