首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在odoo12中将金额转换为NPR格式的单词?

在odoo12中将金额转换为NPR格式的单词?
EN

Stack Overflow用户
提问于 2019-05-02 13:39:52
回答 1查看 229关注 0票数 0

我想转换成文字的NPR格式的金额,但它总是显示在欧元和美分。如何在转换为单词时将其转换为NPR格式。

我已经尝试了所有的方法,lang也,但欧元和美分是无法替代的。我的公司货币是NPR,但无法转换。我有与res.currency相关的currency_id字段。

我尝试过如下代码:

代码语言:javascript
复制
@api.depends('amount')
    def set_amt_in_words(self):
        self.amt_inwords = num2words(self.amount, to = 'currency',  lang = 'en_IN')
        if self.currency_id == 'NPR':
            amt_inwords = str(amt_inwords).replace('Euro', 'rupees')
            amt_inwords = str(amt_inwords).replace('Cents', 'paise')
            amt_inwords = str(amt_inwords).replace('Cent', 'paise')
        self.amt_inwords += '\tonly'
        self.amt_inwords = self.amt_inwords.title()

我想用卢比和派斯输出。

EN

回答 1

Stack Overflow用户

发布于 2019-05-15 11:02:39

试一试

代码语言:javascript
复制
self.env.ref('base.NPR').with_context({'lang': 'en_IN'}).amount_to_text(self.amount)

以下方法属于模型res.currency,负责将货币金额转换为文本(<path_to_v12>/odoo/addons/base/models/res_currency.py):

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55946221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档