import decimal


def _add_percentage_to_progress_bar(progress_bar, count, total):
    """Add the percentage to the progress bar.

    Parameters
    ----------
    progress_bar : str
        The bar component of the progress bar, as built so far.
    count : int
        The number of cycles completed.
    total : int
        The total number of cycles to complete.

    Returns
    -------
    str
        The progress bar, with percentage appended if appropriate.

    """
    operation_is_not_finished = count != total

    if operation_is_not_finished:
        progress = count / total if total > 0 else 1
        percentage = progress * 100
        # Round the percentage down so that it is never shown as 100% before
        # the operation is finished.
        rounded_percentage = decimal.Decimal(percentage).quantize(
            decimal.Decimal("0.0"), rounding=decimal.ROUND_DOWN,
        )
        progress_bar += "  {}%".format(rounded_percentage)

    return progress_bar