我想将金额转换为NPR格式的单词,但始终仅以欧元和美分显示。转换为单词时如何将其更改为NPR格式。
我也尝试了所有方法lang,但是euro和cent无法替换。我的公司货币为NPR,但无法将其转换。我有与currency_id
相关的res.currency
字段。
我尝试了如下代码:
@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()
我想用卢比和帕西输出。
答案 0 :(得分:0)
尝试
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
):
@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