Crate chinese_number

source ·
Expand description

Chinese Number

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"]

Enums

  • 大寫或小寫數字。
  • 根據 五經算術 將大的單位分為 上數 (High)、中數 (Middle)、下數 (Low) 三種類型,再加上現代使用的 萬進 (TenThousand)。
  • ChineseToNumberErrorchinese-to-number
    將中文數字轉成數值時發生的錯誤。
  • The different writing systems used for the Chinese language. Traditional Chinese (繁體中文) or Simple Chinese (简体中文).
  • NumberToChineseErrornumber-to-chinese
    將數值轉成中文數字時發生的錯誤。

Traits

  • ChineseToNumberchinese-to-number
    讓 Rust 程式語言的字串型別擁有將中文數字轉成數值的能力。
  • NumberToChinesenumber-to-chinese
    讓 Rust 程式語言的所有基本數值型別擁有轉成中文數字的能力。

Functions