Giter VIP home page Giter VIP logo

chinese-number's Introduction

Chinese Number

CI

Convert primitive numbers to Chinese numbers, or parse Chinese numbers to primitive numbers.

This crate can convert Rust's primitive number data types to Chinese numbers as strings. For example, 123 can be converted into 一二三, 一百二十三 or 壹佰貳拾參. It supports both Traditional Chinese and Simple Chinese, and it supports different methods to count the scale as well. Also, Chinese numbers in strings can be parsed to primitive number data types.

Example

use chinese_number::{ChineseCase, ChineseCountMethod, ChineseVariant, NumberToChinese, ChineseToNumber};

assert_eq!("一二三", 123i8.to_chinese_naive(ChineseVariant::Traditional, ChineseCase::Lower));

assert_eq!("壹佰貳拾參", 123i8.to_chinese(ChineseVariant::Traditional, ChineseCase::Upper, ChineseCountMethod::TenThousand).unwrap());
assert_eq!("壹佰贰拾叁", 123i8.to_chinese(ChineseVariant::Simple, ChineseCase::Upper, ChineseCountMethod::TenThousand).unwrap());

assert_eq!("一百二十三", 123i8.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::TenThousand).unwrap());

assert_eq!("一極二載三正四澗五溝六穰七秭八垓九京零一億二萬三千四百五十六", 1234567890123456i64.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::Low).unwrap());
assert_eq!("十二穰三千四百五十六秭七千八百九十垓一千二百三十四京五千六百七十八兆九千零一十二億三千四百五十六萬七千八百九十", 123456789012345678901234567890i128.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::TenThousand).unwrap());
assert_eq!("十二萬三千四百五十六京七千八百九十萬一千二百三十四兆五千六百七十八萬九千零一十二億三千四百五十六萬七千八百九十", 123456789012345678901234567890i128.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::Middle).unwrap());
assert_eq!("十二萬三千四百五十六億七千八百九十萬一千二百三十四兆五千六百七十八萬九千零一十二億三千四百五十六萬七千八百九十", 123456789012345678901234567890i128.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::High).unwrap());

assert_eq!("一角二分", 0.12f64.to_chinese(ChineseVariant::Traditional, ChineseCase::Lower, ChineseCountMethod::TenThousand).unwrap());

assert_eq!(123i8, "一二三".to_number_naive().unwrap());

assert_eq!(123i8, "一百二十三".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(-30303i16, "負三萬零三百零三".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(3212345678u32, "三十二億一千二百三十四萬五千六百七十八".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(10010001001001001000u64, "一千零一京零一兆零一十億零一百萬一千".to_number(ChineseCountMethod::TenThousand).unwrap());

assert_eq!(1000000u64, "一兆".to_number(ChineseCountMethod::Low).unwrap());
assert_eq!(1000000000000u64, "一兆".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::Middle).unwrap());
assert_eq!(10000000000000000u64, "一兆".to_number(ChineseCountMethod::High).unwrap());

assert_eq!(120u64, "一百二".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(2300u64, "兩千三".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(34000u64, "三萬四".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(105000u64, "十萬五".to_number(ChineseCountMethod::TenThousand).unwrap());
assert_eq!(150000000u64, "一億五".to_number(ChineseCountMethod::TenThousand).unwrap());

No Std

Disable the default features to compile this crate without std.

[dependencies.chinese-number]
version = "*"
default-features = false
features = ["number-to-chinese", "chinese-to-number"]

Crates.io

https://crates.io/crates/chinese-number

Documentation

https://docs.rs/chinese-number

License

MIT

chinese-number's People

Contributors

amorphobia avatar dependabot[bot] avatar magiclen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cihuuy vuhe

chinese-number's Issues

AGPL'd dependency

Version 0.7.0, corresponding to commit c02aed7, introduced chinese-numerals as a dependency, which is licensed under the AGPL. It's a copyleft license not compatible with the MIT license of this project.

It looks like the dependency isn't actually used. In my (non-expert) understanding, it may be possible to simply remove the dependency from Cargo.toml. However, I'm not sure whether it is actually used, if code was pulled from that project, or if license obligations transfer regardless of how the crate is used.

I discovered this through cargo-deny. It might be helpful to set it up for this project to avoid this happening in the future.

一些不支持的测试用例

有两个确定的问题:

  1. 直接转换繁体 兩千 可以得到正确的结果,对应的一个简体 两千 会出现解析错误
  2. 古语中常用 廿 表示 20,在农历日历表示 20 时常用,直接解析会出现解析错误

还有一个不确定的问题:

口语中常用 三万五 表示 三万五千3_5000 ,解析结果为 30005 我不确定这个用例是否有歧义

其他的类似 两千六 表示 2600一百二 表示 120

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.