Skip to main content

Convert Chinese numerals and Arabic numerals.

Project description

📦 cn2an: Chinese Numerals To Arabic Numerals.

cn2an 是一个将「中文数字」和「阿拉伯数字」互相转化的工具包。

1. 功能

  1. 「中文数字」转「阿拉伯数字」;
  2. 「阿拉伯数字」转「中文数字」;
  3. 「阿拉伯数字」转「大写中文数字」;
  4. 支持命令行调用。

2. 版本支持

  • 理论上支持 python 2.7python 3.6 的所有版本。
  • 实际上仅在 python 2.7.10python 3.6.3 上做过完整测试。
  • 欢迎提交其他版本使用情况到 issues

3. 安装

3.1 使用 pip 安装

pip install cn2an

3.2 从代码库安装

git clone https://github.com/kinggreenhall/cn2an.git
cd cn2an
python setup.py install

4. 使用

4.1 在代码中调用

# 在文件首部引入包
import cn2an

# 查看版本
cn2an.__version__
# output: 0.1.0

1 中文数字转阿拉伯数字

cn2an.cn2an("一百万零五十四")
# output: 1000054

# 支持大写中文数字
cn2an.cn2an("壹佰万零伍十肆")
# output: 1000054

2 阿拉伯数字转中文数字

# 支持数字和字符串两种不同类型的输入
cn2an.an2cn("21024124")
cn2an.an2cn(21024124)
# output: 二千一百零二万四千一百二十四

# 支持小数
cn2an.an2cn("0.414")
cn2an.an2cn(0.414)
# output: 零点四一四

# 支持大写,需要增加额外的参数 True
cn2an.an2cn("21024124", True)
cn2an.an2cn(21024124, True)
# output: 贰仟壹佰零贰万肆仟壹佰贰拾肆

cn2an.an2cn("0.414", True)
cn2an.an2cn(0.414, True)
# output: 零点肆壹肆

4.2 在命令行中使用

1 中文数字转阿拉伯数字

cn2an 一百万零五十四
# output: 1000054

# 支持小数
cn2an 零点四一四
# output: 0.414

# 支持大写
cn2an 壹佰万零伍拾肆
# output: 1000054

2 阿拉伯数字转中文数字

an2cn 21024124
# output: 二千一百零二万四千一百二十四

# 支持小数
an2cn 0.414
# output: 零点四一四

# 支持大写,这里需要增加额外的参数 cap
cn2an 1000054 cap
# output: 壹佰万零伍十肆
an2cn 0.414 cap
# output: 零点肆壹肆

5 TODO

  • 阿拉伯数字转大写中文数字;
  • 关于零的bug;
  • 支持 幺 == 1 的转化;
  • 自动化单元测试。

6 Version

  • v0.0.7 修复 一千五百万零三百零一 等类似格式无法正确转化的问题。
  • v0.1.0 重构代码,兼容 python 2.7。

7 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cn2an-0.1.2.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file cn2an-0.1.2.tar.gz.

File metadata

  • Download URL: cn2an-0.1.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.3

File hashes

Hashes for cn2an-0.1.2.tar.gz
Algorithm Hash digest
SHA256 91977938c666d13d91d4cf5aa70d38e496cf100e5c8850e7cdec5cdcd528b6d7
MD5 481154d798d178aa31fe40c3117d6c8a
BLAKE2b-256 8da4a6a50d3b3d48d27d346cde549554d9fa7c26c2bedb28eaf9845818ff8a9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page