我想转换成文字的NPR格式的金额,但它总是显示在欧元和美分。如何在转换为单词时将其转换为NPR格式。
我已经尝试了所有的方法,lang也,但欧元和美分是无法替代的。我的公司货币是NPR,但无法转换。我有与res.currency相关的currency_id字段。
我尝试过如下代码:
@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()我想用卢比和派斯输出。
发布于 2019-05-15 11:02:39
试一试
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_wordshttps://stackoverflow.com/questions/55946221
复制相似问题