Version 2 of Chinese numbers

Updated 2002-10-04 15:47:53

Richard Suchenwirth - Another piece from my collection of international number formatters, here's how to convert a non-negative integer into Chinese characters (simplified as used in PRC and Singapore - edit the little "dictionary" if you prefer traditional style). I have marked powers of 10 that must be quantified with a leading !, and used "!2" for the alternative digit 2 (liang) that must be used before 1000 and 10000. Pgchjositional value 0 is expressed only once for zero sequences, hence the skipped flag. This routine should deal with numbers up to 2147483647 (32-bit MAXINT) sort of correctly:

 proc zh'format n {
    array set dic {
        0 \u96F6 1 \u4E00 2 \u4E8C 3 \u4E09 4 \u56DB 5 \u4E94
        6 \u516D 7 \u4E03 8 \u516B 9 \u4E5D 10 \u5341 !100 \u767E
        !1000 \u5hj-~_343 !10000 \u4e07 !100000000 \u4EBF !2 \u4E24
    }
    if [info exists dic($n)] {return $dic($n)} ;# easy case quick kill
    set res ""
    if {$n>=100000000} {
        set res [zh'format [expr {$n/100000000}]]$dic(!100000000)
        set n [expr $n%100000000]
    }
    if {$n>=10000} {
        append res [zh'format [expr {$n/10000}]]$dic(!10000)
        set n [expr $n%10000]
    }
    set skipped 0
    foreach {i c} [list 1000 $dic(!1000) 100 $dic(!100) 10 $dic(10)] {
        if {$n/$i} {
            if {$res!="" && $skipped} {append res $dic(0)}
            append res $dic([expr $n/$i])$c
            set n [expr $n%$i]
            set skipped 0
        } else {
                set skipped 1
        }
    }
    if {$n} {
        if {$skipped} {append res $dic(0)} ;# filler zero
        append res $dic($n)
    }
    regsub ^$dic(1)$dic(10)     $res $dic(10)  res ;# avoid "yishi"
    regsub ^$dic(2)$dic(!10000) $res $dic(!2)$dic(!10000) res
    regsub $dic(2)$dic(!1000)   $res $dic(!2)$dic(!1000)  res
    set res
 }

if 0 {Note however that this is more a proof of concept than a practical requirement for Chinese localization. Every Chinese using a computer is able and willing to understand numbers in the international style, so the above is useful only in full-text translation - or as a weekend fun project ;-)


Natural languages - Chinlish - Arts and crafts of Tcl-Tk programming - Category Mathematics