Example #1
0
def _render_decimal(value, places=2, min_places=2):

    if value is not None:
        roundfactor = "0." + "0" * (places - 1) + "1"
        if value < 0:
            roundfactor = "-" + roundfactor

        value = round_decimal(val=value,
                              places=places,
                              roundfactor=roundfactor,
                              normalize=True)
        parts = ("%f" % value).split('.')
        n = parts[0]
        d = ""

        if len(parts) > 0:
            d = parts[1]
        elif min_places:
            d = "0" * min_places

        while len(d) < min_places:
            d = "%s0" % d

        while len(d) > min_places and d[-1] == '0':
            d = d[:-1]

        if len(d) > 0:
            value = "%s.%s" % (n, d)
        else:
            value = n
    return value
Example #2
0
def _render_decimal(value, places=2, min_places=2):

    if value is not None:
        roundfactor = "0." + "0"*(places-1) + "1"
        if value < 0:
            roundfactor = "-" + roundfactor
        
        value = round_decimal(val=value, places=places, roundfactor=roundfactor, normalize=True)
        parts = ("%f" % value).split('.')
        n = parts[0]
        d = ""
    
        if len(parts) > 0:
            d = parts[1]
        elif min_places:
            d = "0" * min_places
        
        while len(d) < min_places:
            d = "%s0" % d
        
        while len(d) > min_places and d[-1] == '0':
            d = d[:-1]
    
        if len(d) > 0:
            value = "%s.%s" % (n, d)
        else:
            value = n
    return value
Example #3
0
    def clean(self, value):
        """
        Normalize the field according to cart normalizing rules.
        """
        cartplaces = config_value('SHOP', 'CART_PRECISION')
        roundfactor = config_value('SHOP', 'CART_ROUNDING')    

        if not value or value == '':
            value = Decimal(0)

        try:
            value = round_decimal(val=value, places=cartplaces, roundfactor=roundfactor, normalize=True)
        except RoundedDecimalError:
            raise forms.ValidationError(_('%(value)s is not a valid number') % {'value' : value})

        return value
Example #4
0
    def clean(self, value):
        """
        Normalize the field according to cart normalizing rules.
        """
        cartplaces = config_value('SHOP', 'CART_PRECISION')
        roundfactor = config_value('SHOP', 'CART_ROUNDING')

        if not value or value == '':
            value = Decimal(0)

        try:
            value = round_decimal(val=value,
                                  places=cartplaces,
                                  roundfactor=roundfactor,
                                  normalize=True)
        except RoundedDecimalError:
            raise forms.ValidationError(
                _('%(value)s is not a valid number') % {'value': value})

        return value