import math


def round_esys_size(power, round_up=False):
    """
    Round nominal power values. Rounding dependent on
    decimal size of power input value.

    Parameters
    ----------
    power : float
        Power value in W
    round_up : bool, optional
        Defines if all values should be rounded up
        (default: False)

    Returns
    -------
    power_round : int
        Rounded power value in W
    """

    assert power >= 0

    if round_up:

        if power > 10000:
            power_round = math.ceil(power/1000)*1000
        elif power > 1000:
            power_round = math.ceil(power/100)*100
        elif power > 100:
            power_round = math.ceil(power/10)*10
        else:
            power_round = math.ceil(power)

    else:  # Open direction for rounding

        if power > 10000:
            power_round = round(power/1000, 0)*1000
        elif power > 1000:
            power_round = round(power/100, 0)*100
        elif power > 100:
            power_round = round(power/10, 0)*10
        else:
            power_round = round(power, 0)

    return power_round