import math


def _ms_to_us(time_ms, interval_us=1e3, nearest_up=True):
    """Convert [ms] into the (not smaller/greater) nearest [us]

    Translate a time expressed in [ms] to the nearest time in [us]
    which is a integer multiple of the specified interval_us and it is not
    smaller than the original time_s.

    Example:
    ::
        _ms_to_us(1.0)       => 1000 [us]
        _ms_to_us(1.0, 1024) => 1024 [us]

        _ms_to_us(1.1)       => 2000 [us]
        _ms_to_us(1.1, 1024) => 2048 [us]

    :param time_ms: time in milliseconds
    :type time_ms: float

    :param interval_us: the result will be an integer multiple o this value
        (default = 1e3)
    :type time_ms: int

    :param nearest_up: convert to not smaller nearest if True, to not greater
        otherwise (default = True)
    :type nearest_up: bool
    """
    if nearest_up:
        return interval_us * int(math.ceil((1e3 * time_ms) / interval_us))
    return interval_us * int(math.floor((1e3 * time_ms) / interval_us))