cnnum

    精确中文数字、混合单位、句子转换与可配置人民币金额

    Download zip
    Version
    0.4.0
    License
    MIT
    Last updated
    7 days ago
    Downloads
    3

    #可执行 API 示例

    增加规范人民币大写金额逆向解析,4000 个金额往返用例。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "financial money parser roundtrip and canonical rejection" {
    for i in -2000..<2000 {
    assert_eq(
    @cnnum.parse_money(@cnnum.format_money(i.to_int64())),
    i.to_int64(),
    )
    }
    for s in ["一元整", "负零元整", "壹元伍分伍分", "壹元"] {
    assert_true(
    try {
    ignore(@cnnum.parse_money(s))
    false
    } catch {
    _ => true
    },
    )
    }
    }

    限制:仅接受本库规范金额格式;非规范、方言或自然语言金额不自动猜测。

    NumberError

    pub suberror NumberError {
    Invalid(String)
    } derive(
    Debug
    )

    NumberSpan

    pub struct NumberSpan {
    start : Int
    end : Int
    kind : String
    source : String
    replacement : String
    } derive(Eq,
    Debug
    )

    Offsets are UTF-16 code units, matching String slicing and browser selection.

    an2cn

    fn an2cn(source : String, mode? : String) -> String raise NumberError

    Format text using wan/yi groups, keeping exact fraction digits and signed zero.

    cn2an

    fn cn2an(source : String, mode? : String) -> String raise NumberError

    Exact textual result. normal adds serial digits and final-unit ellipsis; smart also expands Arabic runs and decimal coefficients of a single unit.

    format_currency

    fn format_currency(source : String, zero_yuan? : Bool, yuan? : String, whole_suffix? : String, prefix? : String, rounding? : String, parentheses? : Bool) -> String raise NumberError

    Text arithmetic; rounding is reject (default), truncate, or half-up.

    format_decimal

    fn format_decimal(source : String, financial? : Bool) -> String raise NumberError

    Unit-based integer part plus up to 64 exact fractional digits. Retains trailing zeros. Leading integer zeros are normalized; explicit negative zero is retained.

    format_decimal_cents

    fn format_decimal_cents(cents : Int64) -> String raise NumberError

    Complement of parse_decimal_cents: always exactly two decimal digits.

    format_digits

    fn format_digits(source : String, financial? : Bool) -> String raise NumberError

    Read a digit sequence, retaining leading zeros, fraction scale and negative zero.

    format_integer

    fn format_integer(n : Int64, financial? : Bool) -> String raise NumberError

    Range: -9,999,999,999,999,999 through +9,999,999,999,999,999.

    format_money

    fn format_money(cents : Int64) -> String raise NumberError

    Exact signed integer cents; no binary floating-point conversion or rounding.

    parse_decimal

    fn parse_decimal(source : String, variants? : Bool) -> String raise NumberError

    Returns an exact ASCII decimal string; never rounds through a floating-point value.

    parse_decimal_cents

    fn parse_decimal_cents(s : String) -> Int64 raise NumberError

    parse_digits

    fn parse_digits(source : String, variants? : Bool) -> String raise NumberError

    Direct digit reading: 一二点三零 -> 12.30; unit words such as 十 are rejected.

    parse_integer

    fn parse_integer(text : String, variants? : Bool) -> Int64 raise NumberError

    Parses canonical output of format_integer (common or financial). Ambiguous colloquialisms such as 一万二 and serial digits 一二三 are rejected.

    parse_money

    fn parse_money(source : String, variants? : Bool) -> Int64 raise NumberError

    Parse the canonical output of format_money, including financial digits.

    transform

    fn transform(source : String, direction? : String, direct? : Bool) -> String raise NumberError

    transform_spans

    fn transform_spans(source : String, direction? : String, direct? : Bool) -> Array[NumberSpan] raise NumberError

    Deterministic lexical conversion, not language understanding. Invalid maximal numeral runs are preserved as a whole. Input limit: 262144 UTF-16 code units.