From c80d26c44d48a3030e4415432d73aa4ea2c39688 Mon Sep 17 00:00:00 2001 From: Ailln Date: Sat, 14 Dec 2019 19:38:44 +0800 Subject: [PATCH] =?UTF-8?q?v0.3.10=20=E6=94=AF=E6=8C=81=E5=88=B0=E4=B8=87?= =?UTF-8?q?=E4=BA=BF=E5=85=86=EF=BC=8C=E5=8D=B310**32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++++--- cn2an/an2cn.py | 4 ++-- cn2an/an2cn_test.py | 5 +++++ cn2an/cn2an.py | 48 +++++++++++++++++++++++++++------------------ cn2an/cn2an_test.py | 5 +++++ cn2an/config.yaml | 33 +++++++++++++++++++++++++++++++ cn2an/version.py | 2 +- setup.py | 2 +- upload_pypi.sh | 4 +++- 9 files changed, 86 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 251963d..24b8574 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![stars](https://img.shields.io/github/stars/Ailln/cn2an.svg)](https://github.com/Ailln/cn2an/stargazers) [![API](https://img.shields.io/badge/API-reference-pink.svg)](https://github.com/Ailln/cn2an/wiki/API) -📦 **`cn2an`** 是一个将 `中文数字` 和 `阿拉伯数字` 快速转化的工具包! +📦 **`cn2an`** 是一个快速转化 `中文数字` 和 `阿拉伯数字` 的工具包! [![](https://award.dovolopor.com?lt=Ailln's&rt=idea&lbc=lightgray&rbc=red<c=red)](https://github.com/Ailln/award) @@ -56,12 +56,14 @@ python setup.py install import cn2an # 查看版本 -cn2an.__version__ -# output: 0.3.9 +print(cn2an.__version__) +# 0.3.10 ``` ### 3.1 `中文数字` => `阿拉伯数字` +> 最大支持到`万亿兆`,即`10**32`。 + ```python import cn2an @@ -89,6 +91,8 @@ print(output) ### 3.2 `阿拉伯数字` => `中文数字` +> 最大支持到`10**32`,即`万亿兆`。 + ```python import cn2an diff --git a/cn2an/an2cn.py b/cn2an/an2cn.py index aa4851d..10a3c3b 100644 --- a/cn2an/an2cn.py +++ b/cn2an/an2cn.py @@ -114,7 +114,7 @@ def integer_convert(self, integer_data, mode): len_integer_data = len(integer_data) if len_integer_data > len(unit_list): - raise ValueError("超出数据范围,最长支持 16 位") + raise ValueError(f"超出数据范围,最长支持 {len(unit_list)} 位") output_an = "" for i, d in enumerate(integer_data): @@ -127,7 +127,7 @@ def integer_convert(self, integer_data, mode): if i > 0 and not output_an[-1] == "零": output_an += numeral_list[int(d)] - output_an = output_an.replace("零零", "零").replace("零万", "万").replace("零亿", "亿").replace("亿万", "亿").strip("零") + output_an = output_an.replace("零零", "零").replace("零万", "万").replace("零亿", "亿").replace("零兆", "兆").strip("零") # 解决「一十几」和「壹拾几」问题 if output_an[:2] in ["一十", "壹拾"]: diff --git a/cn2an/an2cn_test.py b/cn2an/an2cn_test.py index 59bcc6c..fa8e641 100644 --- a/cn2an/an2cn_test.py +++ b/cn2an/an2cn_test.py @@ -16,6 +16,11 @@ def setUp(self): 1000000: ["一百万", "壹佰万", "壹佰万元整"], 1000054: ["一百万零五十四", "壹佰万零伍拾肆", "壹佰万零伍拾肆元整"], 31000054: ["三千一百万零五十四", "叁仟壹佰万零伍拾肆", "叁仟壹佰万零伍拾肆元整"], + 98765432987654329876543298765432: [ + "九千八百七十六万五千四百三十二亿九千八百七十六万五千四百三十二兆九千八百七十六万五千四百三十二亿九千八百七十六万五千四百三十二", + "玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰兆玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰", + "玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰兆玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰元整" + ], -0: ["零", "零", "零元整"], -1: ["负一", "负壹", "负壹元整"], -11: ["负十一", "负拾壹", "负拾壹元整"] diff --git a/cn2an/cn2an.py b/cn2an/cn2an.py index 5af0c5f..efafae2 100644 --- a/cn2an/cn2an.py +++ b/cn2an/cn2an.py @@ -114,31 +114,41 @@ def integer_convert(self, integer_data): else: # 核心 output_integer = 0 - unit_value = 1 - ten_thousand_unit_key = 1 - - for index, int_key in enumerate(reversed(integer_data)): - unit_key = self.conf["number_unit"].get(int_key) + unit = 1 + # 万、亿、万亿、兆、万兆、亿兆、万亿兆 + ten_thousand_unit = 1 + # 万、亿、兆 + max_ten_thousand_unit = 1 + last_unit = 0 + + for index, cn_num in enumerate(reversed(integer_data)): + num = self.conf["number_unit"].get(cn_num) # 数值 - if unit_key < 10: - output_integer += unit_value * unit_key + if num < 10: + output_integer += num * unit # 单位 else: - # 判断出"万、亿" - if unit_key % 10000 == 0: - if unit_key > ten_thousand_unit_key: - ten_thousand_unit_key = unit_key + # 判断出"万、亿、兆" + if num % 10000 == 0: + if num > ten_thousand_unit: + ten_thousand_unit = num + max_ten_thousand_unit = num else: - ten_thousand_unit_key = ten_thousand_unit_key * unit_key - unit_key = ten_thousand_unit_key - - if unit_key > unit_value: - unit_value = unit_key + # 亿兆 + if last_unit < num < max_ten_thousand_unit: + ten_thousand_unit = num * max_ten_thousand_unit + else: + ten_thousand_unit = num * ten_thousand_unit + last_unit = num + num = ten_thousand_unit + + if num > unit: + unit = num else: - unit_value = ten_thousand_unit_key * unit_key + unit = num * ten_thousand_unit - if index == len(integer_data)-1: - output_integer += unit_value + if index == len(integer_data) - 1: + output_integer += unit return int(output_integer) diff --git a/cn2an/cn2an_test.py b/cn2an/cn2an_test.py index d5986b0..b5ee0b7 100644 --- a/cn2an/cn2an_test.py +++ b/cn2an/cn2an_test.py @@ -21,6 +21,11 @@ def setUp(self): "一十一亿一千一百一十一万一千一百一十一": 1111111111, "一百一十一亿一千一百一十一万一千一百一十一": 11111111111, "一千一百一十一亿一千一百一十一万一千一百一十一": 111111111111, + "一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 1111111111111111, + "一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 11111111111111111111, + "一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 111111111111111111111111, + "一千一百一十一亿一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 1111111111111111111111111111, + "一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 11111111111111111111111111111111, "壹": 1, "拾": 10, "拾壹": 11, diff --git a/cn2an/config.yaml b/cn2an/config.yaml index 9ba0cba..e0f6529 100644 --- a/cn2an/config.yaml +++ b/cn2an/config.yaml @@ -28,6 +28,7 @@ number_unit: 仟: 1000 万: 10000 亿: 100000000 + 兆: 10000000000000000 number_low: - 零 @@ -71,6 +72,22 @@ unit_low: - 十 - 百 - 千 + - 兆 + - 十 + - 百 + - 千 + - 万 + - 十 + - 百 + - 千 + - 亿 + - 十 + - 百 + - 千 + - 万 + - 十 + - 百 + - 千 unit_up: - "" @@ -89,3 +106,19 @@ unit_up: - 拾 - 佰 - 仟 + - 兆 + - 拾 + - 佰 + - 仟 + - 万 + - 拾 + - 佰 + - 仟 + - 亿 + - 拾 + - 佰 + - 仟 + - 万 + - 拾 + - 佰 + - 仟 diff --git a/cn2an/version.py b/cn2an/version.py index ceb32f1..ac86f15 100644 --- a/cn2an/version.py +++ b/cn2an/version.py @@ -1 +1 @@ -VERSION = "0.3.9" +VERSION = "0.3.10" diff --git a/setup.py b/setup.py index 596d16c..7332e04 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name="cn2an", - version="0.3.9", + version="0.3.10", author="Ailln", author_email="kinggreenhall@gmail.com", url="https://github.com/Ailln/cn2an", diff --git a/upload_pypi.sh b/upload_pypi.sh index 1d67400..2c3fc27 100755 --- a/upload_pypi.sh +++ b/upload_pypi.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash rm dist/* -python setup.py sdist +python setup.py sdist bdist_wheel + +pip install twine twine upload dist/* \ No newline at end of file