首页 文章

Odoo 12字数

提问于
浏览
0

如何在Odoo 12中使用num2words库来显示发票报告中的总金额?

我在/base/modules/res_currency.py中找到了num2words函数

@api.multi
def amount_to_text(self, amount):
    self.ensure_one()
    def _num2words(number, lang):
        try:
            return num2words(number, lang=lang).title()
        except NotImplementedError:
            return num2words(number, lang='en').title()

    if num2words is None:
        logging.getLogger(__name__).warning("The library 'num2words' is missing, cannot render textual amounts.")
        return ""

    formatted = "%.{0}f".format(self.decimal_places) % amount
    parts = formatted.partition('.')
    integer_value = int(parts[0])
    fractional_value = int(parts[2] or 0)

    lang_code = self.env.context.get('lang') or self.env.user.lang
    lang = self.env['res.lang'].search([('code', '=', lang_code)])
    amount_words = tools.ustr('{amt_value} {amt_word}').format(
                    amt_value=_num2words(integer_value, lang=lang.iso_code),
                    amt_word=self.currency_unit_label,
                    )
    if not self.is_zero(amount - integer_value):
        amount_words += ' ' + _('and') + tools.ustr(' {amt_value} {amt_word}').format(
                    amt_value=_num2words(fractional_value, lang=lang.iso_code),
                    amt_word=self.currency_subunit_label,
                    )
    return amount_words

How to use this? 我是初学者,所以明白了,谢谢!

1 回答

  • 0

    基本上,如果方法返回文本>> return amount_words <<并要求将参数作为数字>> def amount_to_text(self, amount )传递:<<你可以做什么

    考虑到方法是有效的! <<

    @api.one
    def amount_in_words_from_number(self):
        amount_in_number = 12345
        amount_in_words = ''
        #Pass the amount in number as parameter which should return number in text
        amount_in_words = self.amount_to_text(amount_in_number)
        print("Amount in words", amount_in_words)
    

相关问题