コード例 #1
0
ファイル: rounding.py プロジェクト: inasafe/inasafe
def denomination(value, min_nominal=None):
    """Return the denomination of a number.

    :param value: The value.
    :type value: int

    :param min_nominal: Minimum value of denomination eg: 1000, 100.
    :type min_nominal: int

    :return: The new value and the denomination as a unit definition.
    :rtype: tuple(int, safe.unit.definition)
    """
    if value is None or (hasattr(value, 'isNull') and value.isNull()):
        return None, None

    if not value:
        return value, None

    if abs(value) == list(nominal_mapping.keys())[0] and not min_nominal:
        return 1 * value, nominal_mapping[list(nominal_mapping.keys())[0]]

    # we need minimum value of denomination because we don't want to show
    # '2 ones', '3 tens', etc.
    index = 0
    if min_nominal:
        index = int(ceil(log(min_nominal, 10)))

    iterator = list(
        zip(
            list(nominal_mapping.keys())[index:],
            list(nominal_mapping.keys())[index + 1:]))
    for min_value, max_value in iterator:

        if min_value <= abs(value) < max_value:
            return float(value) / min_value, nominal_mapping[min_value]
        elif abs(value) < min_value:
            return float(value), None

    max_value = list(nominal_mapping.keys())[-1]
    new_value = float(value) / max_value
    new_unit = nominal_mapping[list(nominal_mapping.keys())[-1]]
    return new_value, new_unit
コード例 #2
0
ファイル: rounding.py プロジェクト: timlinux/inasafe
def denomination(value, min_nominal=None):
    """Return the denomination of a number.

    :param value: The value.
    :type value: int

    :param min_nominal: Minimum value of denomination eg: 1000, 100.
    :type min_nominal: int

    :return: The new value and the denomination as a unit definition.
    :rtype: list(int, safe.unit.definition)
    """
    if isinstance(value, QPyNullVariant):
        return None

    if not value:
        return value, None

    if abs(value) == nominal_mapping.keys()[0] and not min_nominal:
        return 1 * value, nominal_mapping[nominal_mapping.keys()[0]]

    # we need minimum value of denomination because we don't want to show
    # '2 ones', '3 tens', etc.
    index = 0
    if min_nominal:
        index = int(ceil(log(min_nominal, 10)))

    iterator = zip(
        nominal_mapping.keys()[index:], nominal_mapping.keys()[index + 1:])
    for min_value, max_value in iterator:

        if min_value <= abs(value) < max_value:
            return float(value) / min_value, nominal_mapping[min_value]
        elif abs(value) < min_value:
            return float(value), None

    max_value = nominal_mapping.keys()[-1]
    new_value = float(value) / max_value
    new_unit = nominal_mapping[nominal_mapping.keys()[-1]]
    return new_value, new_unit