Skip to content

Commit

Permalink
v0.3.10 支持到万亿兆,即10**32
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailln committed Dec 14, 2019
1 parent c2025d1 commit c80d26c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 27 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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&ltc=red)](https://github.com/Ailln/award)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -89,6 +91,8 @@ print(output)

### 3.2 `阿拉伯数字` => `中文数字`

> 最大支持到`10**32`,即`万亿兆`
```python
import cn2an

Expand Down
4 changes: 2 additions & 2 deletions cn2an/an2cn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 ["一十", "壹拾"]:
Expand Down
5 changes: 5 additions & 0 deletions cn2an/an2cn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def setUp(self):
1000000: ["一百万", "壹佰万", "壹佰万元整"],
1000054: ["一百万零五十四", "壹佰万零伍拾肆", "壹佰万零伍拾肆元整"],
31000054: ["三千一百万零五十四", "叁仟壹佰万零伍拾肆", "叁仟壹佰万零伍拾肆元整"],
98765432987654329876543298765432: [
"九千八百七十六万五千四百三十二亿九千八百七十六万五千四百三十二兆九千八百七十六万五千四百三十二亿九千八百七十六万五千四百三十二",
"玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰兆玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰",
"玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰兆玖仟捌佰柒拾陆万伍仟肆佰叁拾贰亿玖仟捌佰柒拾陆万伍仟肆佰叁拾贰元整"
],
-0: ["零", "零", "零元整"],
-1: ["负一", "负壹", "负壹元整"],
-11: ["负十一", "负拾壹", "负拾壹元整"]
Expand Down
48 changes: 29 additions & 19 deletions cn2an/cn2an.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions cn2an/cn2an_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def setUp(self):
"一十一亿一千一百一十一万一千一百一十一": 1111111111,
"一百一十一亿一千一百一十一万一千一百一十一": 11111111111,
"一千一百一十一亿一千一百一十一万一千一百一十一": 111111111111,
"一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 1111111111111111,
"一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 11111111111111111111,
"一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 111111111111111111111111,
"一千一百一十一亿一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 1111111111111111111111111111,
"一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一兆一千一百一十一万一千一百一十一亿一千一百一十一万一千一百一十一": 11111111111111111111111111111111,
"壹": 1,
"拾": 10,
"拾壹": 11,
Expand Down
33 changes: 33 additions & 0 deletions cn2an/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ number_unit:
: 1000
: 10000
亿: 100000000
: 10000000000000000

number_low:
-
Expand Down Expand Up @@ -71,6 +72,22 @@ unit_low:
-
-
-
-
-
-
-
-
-
-
-
- 亿
-
-
-
-
-
-
-

unit_up:
- ""
Expand All @@ -89,3 +106,19 @@ unit_up:
-
-
-
-
-
-
-
-
-
-
-
- 亿
-
-
-
-
-
-
-
2 changes: 1 addition & 1 deletion cn2an/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.3.9"
VERSION = "0.3.10"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="cn2an",
version="0.3.9",
version="0.3.10",
author="Ailln",
author_email="[email protected]",
url="https://github.com/Ailln/cn2an",
Expand Down
4 changes: 3 additions & 1 deletion upload_pypi.sh
Original file line number Diff line number Diff line change
@@ -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/*

0 comments on commit c80d26c

Please sign in to comment.