最近工作需要,得把金额转换成大写格式,google了一遍,没发现特别满意的轮子,耗费近一通宵,自己造了一个。python3.6+的,金额超过一亿的就先不管了,业务上不需要。代码于2019年2月份部署到生产环境上,至今运行良好。
#!/usr/bin/env python3
import re
from decimal import Decimal, ROUND_HALF_UP
ZH_HANS_MAP = dict(zip(range(10), "零壹贰叁肆伍陆柒捌玖"))
def verbose_price(cost):
"""convert price to zh_hans
>>> from decimal import Decimal
>>> verbose_price(Decimal(123456.34))
'壹拾贰万叁仟肆佰伍拾陆元叁角肆分'
>>> verbose_price(123456.04)
'壹拾贰万叁仟肆佰伍拾陆元零肆分'
>>> verbose_price(Decimal(123456.40))
'壹拾贰万叁仟肆佰伍拾陆元肆角'
>>> verbose_price(123456.4)
'壹拾贰万叁仟肆佰伍拾陆元肆角'
>>> verbose_price(123406.4)
'壹拾贰万叁仟肆佰零陆元肆角'
>>> verbose_price(123006.4)
'壹拾贰万叁仟零陆元肆角'
>>> verbose_price(100000)
'壹拾万元整'
>>> verbose_price(110000)
'壹拾壹万元整'
>>> verbose_price(110001)
'壹拾壹万零壹元整'
>>> verbose_price(1010001)
'壹佰零壹万零壹元整'
>>> verbose_price(2464.27)
'贰仟肆佰陆拾肆元贰角柒分'
"""
cost = Decimal(cost).quantize(Decimal('0.00'), ROUND_HALF_UP) # 四舍五入保留两位小数
if cost >= 100_000_000:
return "大于等于1亿"
if cost >= 10000:
w = int(cost // 10000)
rest = int(cost % 10000)
r = qian(w) + "万" + qian(rest)
elif cost >= 1:
r = qian(int(cost))
else:
r = ""
if r:
r += "元"
if int(cost) == cost:
r += "整"
else:
j, f = int(cost * Decimal(10)) % 10, int(cost * Decimal(100)) % 10
t = ZH_HANS_MAP[j] + "角" * bool(j) + ZH_HANS_MAP[f] + "分" * bool(f)
t = t.rstrip("零")
if not r:
t = t.lstrip("零")
r += t
return re.sub(r"零{2,}", "零", r.strip("零"))
def qian(c):
ds = [int(i) for i in list(f"{c:04}")]
ts = list("仟佰拾") + [""]
s = "".join(ZH_HANS_MAP[d] + t * bool(d) for d, t in zip(ds, ts))
return s.rstrip("零")
if __name__ == "__main__":
import doctest
rs = doctest.testmod()
if rs.failed == 0:
print("All tests passed ~\nDone.")