pricing/babel_numbers.py
def apply(
            self, value, locale, currency=None, currency_digits=True,
            decimal_quantization=True):
        """Renders into a string a number following the defined pattern.
        Forced decimal quantization is active by default so we'll produce a
        number string that is strictly following CLDR pattern definitions.
        """
        if not isinstance(value, decimal.Decimal):
            value = decimal.Decimal(str(value))

        value = value.scaleb(self.scale)

        # Separate the absolute value from its sign.
        is_negative = int(value.is_signed())
        value = abs(value).normalize()

        # Prepare scientific notation metadata.
        if self.exp_prec:
            value, exp, exp_sign = self.scientific_notation_elements(
                value, locale)

        # Adjust the precision of the fractionnal part and force it to the
        # currency's if neccessary.
        frac_prec = self.frac_prec
        if currency and currency_digits:
            frac_prec = (babel.numbers.get_currency_precision(currency), ) * 2

        # Bump decimal precision to the natural precision of the number if it
        # exceeds the one we're about to use. This adaptative precision is only
        # triggered if the decimal quantization is disabled or if a scientific
        # notation pattern has a missing mandatory fractional part (as in the
        # default '#E0' pattern). This special case has been extensively
        # discussed at
        # https://github.com/python-babel/babel/pull/494#issuecomment-307649969
        if not decimal_quantization or (self.exp_prec and frac_prec == (0, 0)):
            frac_prec = (frac_prec[0], max([frac_prec[1],
                                            get_decimal_precision(value)]))

        # Render scientific notation.
        if self.exp_prec:
            number = ''.join([
                self._quantize_value(value, locale, frac_prec),
                babel.numbers.get_exponential_symbol(locale),
                exp_sign,
                self._format_int(
                    str(exp), self.exp_prec[0], self.exp_prec[1], locale)])

        # Is it a siginificant digits pattern?
        elif '@' in self.pattern:
            text = self._format_significant(value,
                                            self.int_prec[0],
                                            self.int_prec[1])
            a, sep, b = text.partition(".")
            number = self._format_int(a, 0, 1000, locale)
            if sep:
                number += babel.numbers.get_decimal_symbol(locale) + b

        # A normal number pattern.
        else:
            number = self._quantize_value(value, locale, frac_prec)

        retval = ''.join([
            self.prefix[is_negative],
            number,
            self.suffix[is_negative]])

        if u'¤' in retval:
            retval = retval.replace(u'¤¤¤',
                                    babel.numbers.get_currency_name(
                                        currency, value, locale))
            retval = retval.replace(u'¤¤', currency.upper())
            retval = retval.replace(u'¤', babel.numbers.get_currency_symbol(
                currency, locale))

        return retval
def apply(
            self, value, locale, currency=None, currency_digits=True,
            decimal_quantization=True):
        """Renders into a string a number following the defined pattern.
        Forced decimal quantization is active by default so we'll produce a
        number string that is strictly following CLDR pattern definitions.
        """
        if not isinstance(value, decimal.Decimal):
            value = decimal.Decimal(str(value))

        value = value.scaleb(self.scale)

        # Separate the absolute value from its sign.
        is_negative = int(value.is_signed())
        value = abs(value).normalize()

        # Prepare scientific notation metadata.
        if self.exp_prec:
            value, exp, exp_sign = self.scientific_notation_elements(
                value, locale)

        # Adjust the precision of the fractionnal part and force it to the
        # currency's if neccessary.
        frac_prec = self.frac_prec
        if currency and currency_digits:
            frac_prec = (babel.numbers.get_currency_precision(currency), ) * 2

        # Bump decimal precision to the natural precision of the number if it
        # exceeds the one we're about to use. This adaptative precision is only
        # triggered if the decimal quantization is disabled or if a scientific
        # notation pattern has a missing mandatory fractional part (as in the
        # default '#E0' pattern). This special case has been extensively
        # discussed at
        # https://github.com/python-babel/babel/pull/494#issuecomment-307649969
        if not decimal_quantization or (self.exp_prec and frac_prec == (0, 0)):
            frac_prec = (frac_prec[0], max([frac_prec[1],
                                            get_decimal_precision(value)]))

        # Render scientific notation.
        if self.exp_prec:
            number = ''.join([
                self._quantize_value(value, locale, frac_prec),
                babel.numbers.get_exponential_symbol(locale),
                exp_sign,
                self._format_int(
                    str(exp), self.exp_prec[0], self.exp_prec[1], locale)])

        # Is it a siginificant digits pattern?
        elif '@' in self.pattern:
            text = self._format_significant(value,
                                            self.int_prec[0],
                                            self.int_prec[1])
            a, sep, b = text.partition(".")
            number = self._format_int(a, 0, 1000, locale)
            if sep:
                number += babel.numbers.get_decimal_symbol(locale) + b

        # A normal number pattern.
        else:
            number = self._quantize_value(value, locale, frac_prec)

        retval = ''.join([
            self.prefix[is_negative],
            number,
            self.suffix[is_negative]])

        if u'¤' in retval:
            retval = retval.replace(u'¤¤¤',
                                    babel.numbers.get_currency_name(
                                        currency, value, locale))
            retval = retval.replace(u'¤¤', currency.upper())
            retval = retval.replace(u'¤', babel.numbers.get_currency_symbol(
                currency, locale))

        return retval
Renders into a string a number following the defined pattern.
        Forced decimal quantization is active by default so we'll produce a
        number string that is strictly following CLDR pattern definitions.