Files
app-odoo/app_base_chinese/models/res_currency.py
2021-11-11 19:59:33 +08:00

54 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
from odoo import api, fields, models
class ResCurrency(models.Model):
_inherit = 'res.currency'
_order = 'active desc, sequence, name'
sequence = fields.Integer('Sequence', default=10, help="Determine the display order. Sort ascending.")
def rmb_upper(self, value):
"""
人民币大写
传入浮点类型的值返回 unicode 字符串
:param 传入阿拉伯数字
:return 返回值是对应阿拉伯数字的绝对值的中文数字
"""
if self.name != 'CNY':
return
rmbmap = [u"", u"", u"", u"", u"", u"", u"", u"", u"", u""]
unit = [u"", u"", u"", u"", u"", u"", u"", u"", u"", u"", u"亿",
u"", u"", u"", u"", u"", u"", u"", u""]
# 冲红负数处理
xflag = 0
if value < 0:
xflag = value
value = abs(value)
# 先把value 数字进行格式化保留两位小数,转成字符串然后去除小数点
nums = list(map(int, list(str('%0.2f' % value).replace('.', ''))))
words = []
zflag = 0 # 标记连续0次数以删除万字或适时插入零字
start = len(nums) - 3
for i in range(start, -3, -1): # 使i对应实际位数负数为角分
# 大部分情况对应数字不等于零 或者是刚开始循环
if 0 != nums[start - i] or len(words) == 0:
if zflag:
words.append(rmbmap[0])
zflag = 0
words.append(rmbmap[nums[start - i]]) # 数字对应的中文字符
words.append(unit[i + 2]) # 列表此位置的单位
# 控制‘万/元’ 万和元比较特殊如2拾万和2拾1万 无论有没有这个1 万字是必须的
elif 0 == i or (0 == i % 4 and zflag < 3):
# 上面那种情况定义了 2拾1万 的显示 这个是特殊对待的 2拾万一类的显示
words.append(unit[i + 2])
# 元(控制条件为 0 == i )和万(控制条为(0 == i % 4 and zflag < 3))的情况的处理是一样的
zflag = 0
else:
zflag += 1
if words[-1] != unit[0]: # 结尾非‘分’补整字 最小单位 如果最后一个字符不是最小单位(分)则要加一个整字
words.append(u"")
if xflag < 0: # 如果为负数则要在数字前面加上‘负’字
words.insert(0, u"")
return ''.join(words)