Exemplo n.º 1
0
 def parse_money_amount(self, string, currency, maximum=D_MAX):
     group_symbol = self.number_symbols['group']
     decimal_symbol = self.number_symbols['decimal']
     try:
         decimal = Decimal(
             string.replace(group_symbol, '').replace(decimal_symbol, '.'))
     except (InvalidOperation, ValueError):
         raise InvalidNumber(string)
     if group_symbol in string:
         proper = self.format_decimal(decimal, decimal_quantization=False)
         if string != proper and string.rstrip('0') != (proper +
                                                        decimal_symbol):
             # Irregular number format (e.g. `10.00` in German)
             try:
                 decimal_alt = Decimal(
                     string.replace(decimal_symbol,
                                    '').replace(group_symbol, '.'))
             except (InvalidOperation, ValueError):
                 raise AmbiguousNumber(string, [proper])
             else:
                 proper_alt = self.format_decimal(
                     decimal_alt, decimal_quantization=False)
                 raise AmbiguousNumber(string, [proper, proper_alt])
     if maximum is not None and decimal > maximum:
         raise InvalidNumber(string)
     money = Money(decimal, currency).round_down()
     if money.amount != decimal:
         # The input amount exceeds maximum precision (e.g. $0.001).
         raise InvalidNumber(string)
     return money
Exemplo n.º 2
0
 def parse_decimal_or_400(self, s, maximum=D_MAX):
     try:
         r = parse_decimal(s, locale=self)
     except (InvalidOperation, NumberFormatError, ValueError):
         raise InvalidNumber(s)
     if r.quantize(D_CENT) != r:
         raise InvalidNumber(s)
     if maximum is not None and r > maximum:
         raise InvalidNumber(s)
     return r
Exemplo n.º 3
0
 def parse_decimal_or_400(s, *a):
     try:
         return parse_decimal(s, *a, locale=loc)
     except (InvalidOperation, NumberFormatError, ValueError):
         raise InvalidNumber(s)
Exemplo n.º 4
0
def read_decimal_or_400(s):
    try:
        return Decimal(s)
    except (InvalidOperation, ValueError):
        raise InvalidNumber(s)