Giter Club home page Giter Club logo

chinese-number-converter's Introduction

chinese-number-convertor

This is a simple Chinese number converter that converts between Chinese numbers and Arabic numbers.

👍 Quickstart

from cnc import convert

print(convert.chinese2number("五十七")) #57
print(convert.number2chinese(57)) #五十七

👉 chinese2number(string) -> (float|int)

Returns the arabic number representation of given string.

Notes

The function uses a loosely-matching logic, so the given string doesn't need to be confined to a specific pattern.

print(convert.chinese2number("兩千零一十二")) #2012
print(convert.chinese2number("二零一二")) #will also be 2012
print(convert.chinese2number("2012")) #will be, of course, 2012

That being said, please still avoid ambiguous and grammartically incorrect string such as 一兆一 or 一百一千億.

Support character

Support following characters:

  • Normal number: 一...九、十、百、千
  • Large number till 1052-1: 萬、億...極
  • Zero: 零、〇
  • Capital version of all characters above: 壹...玖、拾、佰、仟
  • Arabic number: 1...9、0
  • Simplified version off all characters above: 贰、万...

Arabic numbers were also supported because they will sometimes be mixed with characters, like "1億5000萬".

👉 number2chinese(int) -> (string)

Returns the chinese representation of given number.

Arguments

  • language: string, "T" or "S".
    • Choose between Traditional and Simplified characters.
    • (default is "T")
  • bigNumber: bool, True or False.
    • Output capital version of charaters.
    • (default is False)
print(convert.number2chinese(202)) #兩百零二
print(convert.number2chinese(202, language = "S", bigNumber = True)) #贰佰零贰
  • forceErLian: string, "auto", "force" or "forceNot".
    • Whether to distinguish Er(二) and Lian(兩).
      1. When set to "auto", the output will follow regional convention.
      2. When set to "force", both Traditional and Simplified version will distinguish word usage.
      3. When set to "forceNot", it will always output Er(二) for number "two".
    • (default is "auto")

This will only effect when not using capital number (bigNumber = False). Using capital number will always output 貳/贰.

print(convert.number2chinese(202, language = "T")) #兩百零二
print(convert.number2chinese(202, language = "T", forceErLian = "forceNot")) #二百零二
print(convert.number2chinese(202, language = "S")) #二百零二
print(convert.number2chinese(202, language = "S", forceErLian = "force")) #两百零二

Notes

This function uses "萬進" logic when dealing with larger number (>108), which basically means that every 4 digits will be treated as a group.

This is the most common logic to deal with large numbers, and can support up to 1052-1.

chinese-number-converter's People

Contributors

davidhjong avatar kianmeng avatar nrchan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

kianmeng

chinese-number-converter's Issues

能不能兼容 “廿”、“卅”?

很棒的函数!在Sublime Text 4 中作为插件也运行很好(直接copy到Package/User目录下),和 Reg Replace 插件的plugin也协调工作很好。
全角中文的 “0-9”能识别转换吗?
在转换某些网文的章节编号时,发现有些用“廿niàn”、“卅sà”代替了20、30,如果能兼容就更方便了。谢谢!
㊟ 至于40卌xì、50圩、60圆、70进、80枯,等很少见。

Bug Report: pypi 的新版库是空的

pip install --upgrade ChineseNumberUtils 安装1.1.1后没有文件。安装环境 MacOS 11.7 python3.10。(删除后重新安装也一样)
finder capture:

截屏2023-04-20 17 36 40

PS version 1.0.3
print(convert.chinese2number("两百八十")) # 简体中文“两”

输出是180,我看了源码,觉得这种错误真的很隐蔽啊(输出了结果,还是整数,骗过了数据类型检查器——就是6万7那个)。

另外新版本功能挺好的,MIT许可也很NICE!🙏

建议:chinese2number 的输入检查

我用chinese2number的输入主要来自正则表达式的搜索结果,偶尔会有一些不太规范的情况,比如“百八十”人、十之“八九”、“三五十”个,不管“三七二十一”,这些都是日常说法,偶尔会出现在需要转换的数字序列中,此时,双引号中的数字并不符合转数字功能的语言表达习惯,这种情况是不是可以抛出个异常?当然不转换是我需要的。谢谢!!

bug report 六万七

应该是 67000,程序的返回值是60007.0。六百七、六千七都是正确的。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.