示例#1
0
def format_decimal(interval, value):
    """Return formatted decimal according to interval decimal place

    For example:
    interval = 0.33 (two decimal places)
    my_float = 1.1215454
    Return 1.12 (return only two decimal places as string)
    If interval is an integer return integer part of my_number
    If my_number is an integer return as is
    """
    interval = get_significant_decimal(interval)
    if isinstance(interval, Integral) or isinstance(value, Integral):
        return add_separators(int(value))
    if interval != interval:
        # nan
        return str(value)
    if value != value:
        # nan
        return str(value)
    decimal_places = len(str(interval).split('.')[1])
    my_number_int = str(value).split('.')[0]
    my_number_decimal = str(value).split('.')[1][:decimal_places]
    if len(set(my_number_decimal)) == 1 and my_number_decimal[-1] == '0':
        return my_number_int
    formatted_decimal = (add_separators(int(my_number_int))
                         + decimal_separator()
                         + my_number_decimal)
    return formatted_decimal
示例#2
0
def humanize_min_max(min_value, max_value, interval):
    """Return humanize value format for max and min.

    If the range between the max and min is less than one, the original
    value will be returned.

    :param min_value: Minimum value
    :type min_value: int, float

    :param max_value: Maximim value
    :type max_value: int, float

    :param interval: The interval between classes in the
            class list where the results will be used.
    :type interval: float, int

    :returns: A two-tuple consisting of a string for min_value and a string for
            max_value.
    :rtype: tuple
    """
    current_interval = max_value - min_value
    if interval > 1:
        # print 'case 1. Current interval : ', current_interval
        humanize_min_value = add_separators(int(python2_round(min_value)))
        humanize_max_value = add_separators(int(python2_round(max_value)))

    else:
        # print 'case 2. Current interval : ', current_interval
        humanize_min_value = format_decimal(current_interval, min_value)
        humanize_max_value = format_decimal(current_interval, max_value)
    return humanize_min_value, humanize_max_value
示例#3
0
def format_decimal(interval, value):
    """Return formatted decimal according to interval decimal place

    For example:
    interval = 0.33 (two decimal places)
    my_float = 1.1215454
    Return 1.12 (return only two decimal places as string)
    If interval is an integer return integer part of my_number
    If my_number is an integer return as is
    """
    interval = get_significant_decimal(interval)
    if isinstance(interval, Integral) or isinstance(value, Integral):
        return add_separators(int(value))
    if interval != interval:
        # nan
        return str(value)
    if value != value:
        # nan
        return str(value)
    decimal_places = len(str(interval).split('.')[1])
    my_number_int = str(value).split('.')[0]
    my_number_decimal = str(value).split('.')[1][:decimal_places]
    if len(set(my_number_decimal)) == 1 and my_number_decimal[-1] == '0':
        return my_number_int
    return (add_separators(int(my_number_int)) + decimal_separator() +
            my_number_decimal)
示例#4
0
def humanize_min_max(min_value, max_value, interval):
    """Return humanize value format for max and min.

    If the range between the max and min is less than one, the original
    value will be returned.

    :param min_value: Minimum value
    :type min_value: int, float

    :param max_value: Maximim value
    :type max_value: int, float

    :param interval: The interval between classes in the
            class list where the results will be used.
    :type interval: float, int

    :returns: A two-tuple consisting of a string for min_value and a string for
            max_value.
    :rtype: tuple
    """
    current_interval = max_value - min_value
    if interval > 1:
        # print 'case 1. Current interval : ', current_interval
        humanize_min_value = add_separators(int(round(min_value)))
        humanize_max_value = add_separators(int(round(max_value)))

    else:
        # print 'case 2. Current interval : ', current_interval
        humanize_min_value = format_decimal(current_interval, min_value)
        humanize_max_value = format_decimal(current_interval, max_value)
    return humanize_min_value, humanize_max_value
示例#5
0
    def test_format_int(self):
        """Test formatting integer."""
        lang = locale()

        number = 10000000
        formatted_int = add_separators(number)
        if lang == 'id':
            expected_str = '10.000.000'
        else:
            expected_str = '10,000,000'
        self.assertEqual(expected_str, formatted_int)

        number = 1234
        formatted_int = add_separators(number)
        if lang == 'id':
            expected_str = '1.234'
        else:
            expected_str = '1,234'
        self.assertEqual(expected_str, formatted_int)
示例#6
0
    def test_format_int(self):
        """Test formatting integer"""
        lang = locale()

        number = 10000000
        formatted_int = add_separators(number)
        if lang == 'id':
            expected_str = '10.000.000'
        else:
            expected_str = '10,000,000'
        self.assertEqual(expected_str, formatted_int)

        number = 1234
        formatted_int = add_separators(number)
        if lang == 'id':
            expected_str = '1.234'
        else:
            expected_str = '1,234'
        self.assertEqual(expected_str, formatted_int)