美分未转换

时间:2018-11-08 07:13:46

标签: odoo odoo-10 converters

将实际金额转换为可以正常工作的单词,但美分未转换,这是我的.py代码

def _get_amount_in_words(self, amount_total):

    # TODO: merge, refactor and complete the amount_to_text and amount_to_text_en classes

    amount_in_words = amount_to_text_en.amount_to_text(math.floor(amount_total), lang='en', currency='')

    amount_in_words = amount_in_words.replace(' and Zero Cent', '') # Ugh

    decimals = amount_total % 1

    if not float_is_zero(decimals, precision_digits=2):

        amount_in_words += _(' and %s ') % str(int(round(float_round(decimals*100, precision_rounding=1))))

    return amount_in_words



@api.onchange('amount_total')

def _onchange_amount_total(self):

    if hasattr(super(InvoiceTemplate, self), '_onchange_amount_total'):

        super(InvoiceTemplate, self)._onchange_amount_total()

    self.amount_in_words = self._get_amount_in_words(self.amount_total)

我得到的结果是: “五百七十八美分”

1 个答案:

答案 0 :(得分:2)

首先,您需要导入此文件。

import logging
import logging.config
import sys

class _ExcludeErrorsFilter(logging.Filter):
    def filter(self, record):
        """Filters out log messages with log level ERROR (numeric value: 40) or higher."""
        return record.levelno < 40


config = {
    'version': 1,
    'filters': {
        'exclude_errors': {
            '()': _ExcludeErrorsFilter
        }
    },
    'formatters': {
        'my_formatter': {
            'format': '(%(process)d) %(asctime)s %(name)s (line %(lineno)s) | %(levelname)s %(message)s'
        }
    },
    'handlers': {
        'console_stderr': {
            # Directs log messages with log level ERROR or higher to stderr
            'class': 'logging.StreamHandler',
            'level': 'ERROR',
            'formatter': 'my_formatter',
            'stream': sys.stderr
        },
        'console_stdout': {
            # Directs log messages with log level lower than ERROR to stdout
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'formatter': 'my_formatter',
            'filters': ['exclude_errors'],
            'stream': sys.stdout
        },
        'file': {
            # Directs all log messages to a file
            'class': 'logging.FileHandler',
            'level': 'DEBUG',
            'formatter': 'my_formatter',
            'filename': 'my.log',
            'encoding': 'utf8'
        }
    },
    'root': {
        'level': 'NOTSET',
        'handlers': ['console_stderr', 'console_stdout', 'file']
    },
}

logging.config.dictConfig(config)

然后使用写功能将要转换为文本的金额。

from odoo.tools import amount_to_text_en