数字向中文转换[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 shawls 的 blog

public function chinanum(byval num as string) as string
on error goto chinanumerr
chinanum = ""

dim str_tmp_cn as string
dim str_tmp_zs as string
dim str_tmp_xs as string
dim i as long

if vba.trim(num) = "" then
    goto chinanumerr
end if

for i = 1 to vba.len(num) step 1
     select case vba.mid$(num, i, 1)
         case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."
         case else
              goto chinanumerr
     end select
next i

if num like "*.*" then
    if num like "*.*.*" then
        goto chinanumerr
    end if
    i = vba.instr(1, num, ".", vbtextcompare)
    str_tmp_zs = vba.left(num, i - 1)
    str_tmp_xs = vba.right(num, vba.len(num) - i)


    str_tmp_zs = zstostr(str_tmp_zs)
    str_tmp_xs = xstostr(str_tmp_xs)
   
   
    if str_tmp_zs = "" then
        str_tmp_cn = "零"
    else
        str_tmp_cn = str_tmp_zs
    end if

    if str_tmp_xs <> "" then
        str_tmp_cn = str_tmp_cn & "点" & str_tmp_xs
    end if

end if
goto chinanumok

chinanumok:
    if str_tmp_cn <> "" then
        let chinanum = str_tmp_cn
    else
        goto chinanumerr
    end if
    goto chinanumexit

chinanumerr:
    err.clear
    chinanum = ""
    goto chinanumexit
   
chinanumexit:
    'clear all money
    str_tmp_cn = ""
    str_tmp_zs = ""
    str_tmp_xs = ""
    i = 0
    exit function
   
end function

private function zstostr(byval str_zs as string) as string
on error goto zstostrerr
     if not isnumeric(str_zs) or str_zs like "*.*" or str_zs like "*-*" then
          if trim(str_zs) <> "" then
              goto zstostrerr
          end if
     end if
    
     if vba.len(str_zs) > 16 then
         let str_zs = vba.left(str_zs, 16)
     end if
    
     dim intlen as integer, intcounter as integer
     dim strch as string, strtempch as string
     dim strseqch1 as string, strseqch2 as string
     dim str_zs2ch as string
     str_zs2ch = "零壹贰叁肆伍陆柒捌玖"
     strseqch1 = " 拾佰仟 拾佰仟 拾佰仟 拾佰仟"
     strseqch2 = " 万亿兆"
     str_zs = cstr(cdec(str_zs))
     intlen = len(str_zs)
     for intcounter = 1 to intlen
          strtempch = mid(str_zs2ch, val(mid(str_zs, intcounter, 1)) + 1, 1)
          if strtempch = "零" and intlen <> 1 then
               if mid(str_zs, intcounter + 1, 1) = "0" or (intlen - intcounter + 1) mod 4 = 1 then
                    strtempch = ""
               end if
          else
               strtempch = strtempch & trim(mid(strseqch1, intlen - intcounter + 1, 1))
          end if
          if (intlen - intcounter + 1) mod 4 = 1 then
               strtempch = strtempch & mid(strseqch2, (intlen - intcounter + 1) \ 4 + 1, 1)

本文关键:数字,中文,转换
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top